批处理文件根据循环计数器的值复制不同的文件

批处理文件根据循环计数器的值复制不同的文件

我想将数据文件和相关批处理文件复制到新目录。我想设置一个条件,即对于第二个目录,从第一个目录中复制一个不同的批处理文件。我一直在努力寻找一个正确语法的示例,以识别条件中的循环计数器。看来诀窍是使用“do if”而不是“do”

下面是使用建议的修改的虚拟示例@iTwasnTme...
在名为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

得到的命令窗口输出是:

  >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

答案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

  • 输出/结果:
 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\

我在 bat 中的命令中添加了一个echo,这反映出循环覆盖正确发生,但我看不清楚:

1.使用名称Data_A.txtData_B.txt和创建的文件Bat_B.txt未在循环和/或命令中使用/引用,因为它们未在任何变量中定义。

2.您的问题并未说明文件夹中是否存在名为File_A.txt“File_B.txt Bat_B.txt”的文件。and

3.我建议您在循环提交文件之前检查运行时创建的文件的名称以及变量中定义的文件的名称/存在性。

4.您可以使用下面这个版本亲自测试上述要点...

@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

  • 输出/结果:
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

观察:检查/并以某种方式输入您的代码,使您更容易检查上述备注,我相信您将很容易在循环中找到解决方案,否则,编辑您的问题,我们会看到某种方法来推进解决方案......

相关内容