batch file - How to search for folders with specific naming convention and copy them to different location along with its contents? -


i have directory f:\input various subfolders in it.

i need copy folders has same naming convention such india012345.zip, india09876.zip, etc. these folders unusual .zip in folder name , not zip files!

i need copy folders along contents (subfolders , files) different location.

cd /d f:\input\ /f "tokens=*" %%i in ('dir /s /b /a:d "india*"') echo|xcopy "%%i" "e:\output\" /s 

this present code copies contents not main folders india012345.zip.

folder structure of f:\input:

  • country
    • state
      • 0021
        • op 1
          • qwerty
            • india09876.zip (folder, not compressed)
              • 31-jun-2016
                • xml , pdf files
                • vcxz
                  • 03-aug-2016.zip (file, compressed)
      • 0031
        • op 1
          • india1234.zip (folder, not compressed)
            • 31-jun-2016
              • xml , pdf files
              • vcxz
                • 04-aug-2016.zip (file, compressed)

india*.zip unique in terms of nos.

it not possible rename. please provide solution copy follows:

folder e:\output should contain after batch execution:

  • india09876.zip
    • 31-jun-2016
      • xml , pdf files
      • vcxz
        • 03-aug-2016.zip
  • india1234.zip
    • 31-jun-2016
      • xml , pdf files
      • vcxz
        • 04-aug-2016.zip

the india*.zip directory name must specified in destination path , more options of command xcopy should used /i automatically create destination folder, too.

@echo off /f "delims=" %%i in ('dir "f:\input\india*.zip" /a:d /b /s 2^>nul') xcopy "%%i" "e:\output\%%~nxi\" /c /h /i /k /q /r /s /y >nul 

it easy possible remove unusual .zip folder names during copying process using:

@echo off /f "delims=" %%i in ('dir "f:\input\india*.zip" /a:d /b /s 2^>nul') xcopy "%%i" "e:\output\%%~ni\" /c /h /i /k /q /r /s /y >nul 

%%~ni used instead of %%~nxi rid of "file extension" folder name.

for understanding used commands , how work, open command prompt window, execute there following commands, , read entirely pages displayed each command carefully.

  • dir /?
  • echo /?
  • for /?
  • xcopy /?

and read microsoft article using command redirection operators explanation of >nul.

2^>nul 2>nul redirection operator > escaped ^ apply 2>nul on execution of command dir instead of interpreting redirection on parsing for command line result in exit of batch processing because of syntax error.

2>nul used on execution of dir results in redirecting possible error message output dir stderr device nul suppress it. command dir outputs confusing error message file not found standard error stream if can't find folder matching folder name wildcard pattern india*.zip.


Comments