0

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
Io-oI
  • 9,237
Markm0705
  • 107

1 Answers1

1
@echo off 

rem make some dummy files

    echo >> Data_A.txt
    echo >> Data_B.txt
    echo >> Bat_A.txt
    echo >> Bat_B.txt

rem set run numbers in an array

    set run[0]=1
    set run[1]=2

rem set substitutions

    set A=File_A.txt
    set B=File_B.txt
    set C=Bat_A.txt
    set D=Bat_C.txt

rem make run directories and copy data files

    setlocal EnableDelayedExpansion

    for /l %%n in (0,1,1) do (
        echo\ md Run_!run[%%n]!
        echo\ copy %A% .\Run_!run[%%n]!\
        echo\ copy %B% .\Run_!run[%%n]!\
      )

rem copy batch files to run directories

    for /l %%n in (0,1,1) do (

      if %%n == 0 (
         echo\ 0 == copy %C% .\Run_!run[%%n]!\
       ) else (
         echo\ 1 == copy %D% .\Run_!run[%%n]!\
       )
    )

    EndLocal

  • Outputs/Results:
 md Run_1
 copy File_A.txt .\Run_1\
 copy File_B.txt .\Run_1\
 md Run_2
 copy File_A.txt .\Run_2\
 copy File_B.txt .\Run_2\
 0 == copy Bat_A.txt .\Run_1\
 1 == copy Bat_C.txt .\Run_2\

I've added an echo to your commands in bat, this reflects that loop overrides occur correctly, but I don't see it clearly:

1. Files created with names Data_A.txt, Data_B.txt and Bat_B.txt are not used/referenced in loops and/or commands because they are not defined in any variable.

2. Your question does not state whether your files named File_A.txt, File_B.txtandBat_B.txt` exist in the folder.

3. I suggest that you check the names of the files created at runtime and the names/existence of the files defined in variables before submitting them in a loop.

4. You can test the above points for yourself using this edition below...

@echo off

rem make some dummy files

echo >> Data_A.txt
echo >> Data_B.txt
echo >> Bat_A.txt
echo >> Bat_B.txt

rem set run numbers in an array

set run[0]=1
set run[1]=2

rem set substitutions

set A=File_A.txt
set B=File_B.txt
set C=Bat_A.txt
set D=Bat_C.txt

rem 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]!\
   )

rem copy batch files to run directories

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


  • Outputs/Results:
The system cannot find the file specified.
ERROR: copy File_A.txt .\Run_1\
The system cannot find the file specified.
ERROR: copy File_B.txt .\Run_1\
The system cannot find the file specified.
ERROR: copy File_A.txt .\Run_2\
The system cannot find the file specified.
ERROR: copy File_B.txt .\Run_2\
        1 file(s) copied.
The system cannot find the file specified.
ERROR: if 1 == copy Bat_C.txt .\Run_2\
Folder PATH listing for volume VBOX_Share
Volume serial number is 02B0B4E0 0100:0007
Z:.
|   Data_A.txt
|   Data_B.txt
|   Bat_B.txt
|   Bat_A.txt
|   Q1727531.cmd
|
+---Run_2
\---Run_1
        Bat_A.txt

Obs.: Check/and type your code in a way and make it easier for you to check the above remarks and I believe that you will easily have a solution in your loop, otherwise, edit your question and we will see some way, to advance the solution...

Io-oI
  • 9,237