I'm wanting to copy data files and associated batch files to new directories. I want to set a condition where for the second directory created a different batch file is copied from the first. I was struggling to find an example of the correct syntax for the and if statement that recognizes the loop counter in the condition. It seems the trick was do use 'do if' rather than just 'do'
Dummy example below using suggested edits by @iTwasnTme...
in a batch file named test2.bat
@echo off
rem 1 make some dummy files
echo >> Data_A.txt
echo >> Data_B.txt
echo >> Bat_A.txt
echo >> Bat_B.txt
rem 2 set run numbers in an array
set run[0]=1
set run[1]=2
rem 3 set substitutions
set A=Data_A.txt
set B=Data_B.txt
set C=Bat_A.txt
set D=Bat_B.txt
rem 4 make run directories and copy data files
setlocal EnableDelayedExpansion
for /l %%n in (0,1,1) do (
md Run_!run[%%n]!
copy %A% .\Run_!run[%%n]!\ || echo/ERROR: copy %A% .\Run_!run[%%n]!\
copy %B% .\Run_!run[%%n]!\ || echo/ERROR: copy %B% .\Run_!run[%%n]!\
)
for /l %%n in (0,1,1) do if %%n == 0 (
copy %C% .\Run_!run[%%n]!\ || echo/ERROR: if 0 == copy %C% .\Run_!run[%%n]!
) else (
copy %D% .\Run_!run[%%n]!\ || echo/ERROR: if 1 == copy %D% .\Run_!run[%%n]!
)
tree /f /a
endlocal
The resulting command window output is:
>test2.bat
A subdirectory or file Run_1 already exists.
1 file(s) copied.
1 file(s) copied.
A subdirectory or file Run_2 already exists.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
1 file(s) copied.
Folder PATH listing for volume Windows
Volume serial number is F0A7-2A65
C:.
| Bat_A.txt
| Bat_B.txt
| Data_A.txt
| Data_B.txt
| test2.bat
|
+---Run_1
| Bat_A.txt
| Data_A.txt
| Data_B.txt
|
\---Run_2
Bat_B.txt
Data_A.txt
Data_B.txt