1

I have a production mapping where the folder does have more files to copy from one shared location to the other location.

Location A: D:\UK\UK0623\Path\Temp_Variable\ORIGINAL\test1.pdf,test 2.pdf,.....

Location B: D:\UK\UK0723\Path\Temp_Variable\Reissue\

As you see the above challenge, need to work on 3 steps

  1. Get parent folder path and child folder path from the User and save it in separate Variable till

parent path: D:\UK\UK0623\Path

child path: D:\UK\UK0723\Path

  1. from the Parent path, select and save the first name/folder name as a TEMP variable for example: D:\UK\UK0623\Path\Temp_Variable

  2. Loop thru the TEMP_variable, because we have 100's of folder name available as SNO_01,SNO_02,etc..

  3. get into the Original folder (ParentPath + temp variable + \Original Folder) and copy the files inside Original folder

for example: D:\UK\UK0623\Path\Temp_Variable\Original...

  1. to the child folder path, use TEMP variable into the path then, Create a Reissue folder, then paste the files in it (ChildPath + temp variable + \Reissue Folder)

for example: D:\UK\UK0723\Path\Temp_Variable\Reissue...

Please note: there are 100's of Serial numbers or folders are there in prod.

Reissue folder needs to create runtime for every serial number SNO_01, SNO_02....

I tried the below script,

@echo off
setlocal enabledelayedexpansion
pushd D:\Uk
SET CURRENT_DIR=%cd%
SET PARENT_DIR=%CURRENT_DIR%\UKRR_0623
SET CHILD_DIR=%CURRENT_DIR%\UKRR_0723
SET TEMP_VAR=SNO_01



echo %CURRENT_DIR%
echo %PARENT_DIR%
echo %CHILD_DIR%


for %%i in ("%CURRENT_DIR%\UKRR_0623\%TEMP_VAR%\Original*.*") do (
copy "%%i" "%CURRENT_DIR%\UKRR_0723\%TEMP_VAR%\Reissue")

echo backup completed
pause

it shows the directory path syntax error

EDIT:

@echo off
setlocal enabledelayedexpansion
pushd D:\Uk
SET CURRENT_DIR=%cd%
SET PARENT_DIR=%CURRENT_DIR%\UKRR_0623
SET CHILD_DIR=%CURRENT_DIR%\UKRR_0723

SET TEMP_VARS=SNO_01 SNO_02


for %%i in (%TEMP_VARS%) do (
  xcopy /s "%PARENT_DIR%\%TEMP_VARS%\Original\*.*" 
"%CHILD_DIR%\%TEMP_VARS%\Reissue"
) 
pause

D:\Uk>(xcopy /s "D:\Uk\UKRR_0623\SNO_01 SNO_02\Original*.*" "D:\Uk\UKRR_0723\SNO_01 SNO_02\Reissue" )File not found - . 0 File(s) copied

5
  • "I tried the below batch script" ... and? What happened? What didn't happen? What were you expecting to happen? Do you have any error messages or strange behaviors you can share? Please be mindful that only you can see your screen :) Commented Jul 6, 2023 at 13:15
  • @MathiasR.Jessen, path syntax error happened Commented Jul 6, 2023 at 13:19
  • Why is this tagged "powershell"? Commented Jul 6, 2023 at 13:28
  • still tagged to check any chance with windows batch file Commented Jul 6, 2023 at 13:30
  • You are defining variables wrong, you are using them wrong, and you use a mixture of correct and false folder separators. See set /? (and as it isn't obvious at a first glance: NO SPAC§ES around = (you are defining %CURRENT_DIR % etc)) Commented Jul 6, 2023 at 14:09

1 Answer 1

1

First of all, your script CAN'T work : wrong set syntax, wrong usage of variables, other small mistakes...

1 - Why did you start with \` ? replace it with only @echo off (same thing goes for the last line, only use pause)

2 - Your sets are wrong too, you need to write them with the syntax (no spaces between the code, the equal and the value + no need to write everything between % its for the variables) :

set CODE=VALUE

3 - To use a variable (defined with set) you just need to write the code between %. To print the content of a variable you can do :

echo %code%

And you'll see the output below when you run the script

VALUE

4 - Another small mistake, when you use a path, write it between double quotes (to prevent errors with white spaces). So to change directory to your CURRENT_DIR :

cd "%CURRENT_DIR%"

5 - Your for loop looks good but be sure to have all your copy on the same line and use parenthesis to be sure it will count everyting :

for %%i in ("%PARENT_DIR%\i\Original\*.txt") do (
      copy "%%i" "%CHILD_DIR%\i\Reissue"
) 

If you change everything as I said it should work (don't forget to edit your question to show us the result).

Feel free to ask for further explanations !

PS : don't forget to mark this as the answer if it solved your problem :)

EDIT :

In answer at your comment :

the temporary variable or Directory should change is SNO_01 from both source and destination path, I guess need to use for loop based on the SNO_01,SNO_02,SNO_03, etc...then I need to xcopy by changing the path, Can you help on how to loop this in both source and destination path

You need to loop through different names/values(and copy recursively the files in these folders), to do it, just place the xcopy command in the loop and use the loop variable %%i. In order to do it just do this :

for %%i in (SNO_01 SNO_02 SNO_03 SNO_0N) do (
      xcopy "%CURRENT_DIR%\%%i\Original" "%CURRENT_DIR%\%%i\Reissue"
) 

The values between the parenthesis needs to be separated by spaces only and you can replace it by a variable to have a better format:

SET TEMP_VARS=SNO_01 SNO_02 SNO_03 SNO_0N

for %%i in (%TEMP_VARS%) do (
      xcopy "%CURRENT_DIR%\%%i\Original" "%CURRENT_DIR%\%%i\Reissue"
) 
Sign up to request clarification or add additional context in comments.

11 Comments

thanks for sharing notes, I have made changes but still it fails, made description to make more clear to understand
Have you tried to replace all your / by \ ? It should solve some of your errors. And make sure to put all your for loop in one line : for %%i in ("%yourPath%") do ( (what comes next can be on a new line)
I have tried as advised, unable to fix this, I directly mentioned the path to create new directory "Resissue" in 0723 and copy and paste from 0723 from Original(existing) directory, modified code in code section
@AntonyPrincePeter another solution is to use xcopy /s "Original" "Resissue". If your goal is only to copy recursively the files in the "Original" folder to the "Resissue". Because using copy you'll not create the folders. The xcopy command does it, and the /s option does it recursively. (If you want to try it just replace all your for loop by the line above). Have a great day :)
Thanks @Thomas Montigny, the temporary variable or Directory should change is SNO_01 from both source and destination path, I guess need to use for loop based on the SNO_01,SNO_02,SNO_03, etc...then I need to xcopy by changing the path, Can you help on how to loop this in both source and destination path
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.