Apply windows batch nested copy folder

Author: Zhu Jincan
Source: clever101's column

  Use a windows batch file to copy multiple subfolders under one folder to another folder, the code is as follows:

em 作者:朱金灿
rem 联系方式:[email protected]
rem 实现本地文件夹拷贝,具体是将一个指定目录下的所有文件复制到另一个指定目录下

rem 开始copy

@echo off

for  /l %%i in (2001,1,2022)  do (
     for /l %%j in (4,1,5) do (
     echo G:\MODIS-2001—2022\%%i\%%j
     
      rem 创建文件夹
      if not exist H:\TestData\modis\bak\%%i md H:\TestData\modis\bak\%%i   
      if not exist H:\TestData\modis\bak\%%i\%%j md H:\TestData\modis\bak\%%i\%%j

      xcopy G:\MODIS-2001—2022\%%i\%%j H:\TestData\modis\bak\%%i\%%j\ /s/y
      )
)

pause

  You can see that two layers of for loops are used above. If I change the code to the following:

@echo off

for  /l %%i in (2001,1,2022)  do (
     for /l %%j in (4,1,5) do (
     rem 要复制的文件夹
     set SOUECE=G:\MODIS-2001—2022\%%i\%%j
     rem 复制到的目录
     rem set DESTINATION=H:\TestData\modis\bak\%%i\%%j

      rem 创建文件夹
      if not exist H:\TestData\modis\bak\%%i md H:\TestData\modis\bak\%%i   
      if not exist H:\TestData\modis\bak\%%i\%%j md H:\TestData\modis\bak\%%i\%%j

      echo start copy %SOUECE% to %DESTINATION%
      xcopy %SOUECE% %DESTINATION%\ /s/y
      )
)

pause

  The result is that the circular copy is not realized, but the files in the directory 2022\5 are copied. Why is this?
References:
1. Batch command to copy folders and sub-level files

Guess you like

Origin blog.csdn.net/clever101/article/details/130256186