How to do a batch move and keep folder structure

I have a folder with many subfolders and files.

I need to move all files with extension *.gz to to other folder, but keeping the directory structure.

C:\TestA\Folder1\file.gz      -->  C:\TestB\Folder1\file.gz
C:\TestA\Folder2\file2.gz     -->  C:\TestB\Folder2\file2.gz
C:\TestA\Folder3\file3.gz     -->  C:\TestB\Folder3\file3.gz

How can I achieve this?

I've tried with a FOR and MOVE command, but can't get this to work. I'm using Windows.


Solution 1:

enter image description here

You can make an attempt with RoboCopy, where execution shows you the files that will be copied/moved, using with the switch /L or without /L

๐Ÿคจ   Test: RoboCopy.exe "Z:\TestA\." "Z:\TestB\." *.gz /MOV /FP /NP /IS /Z /E /L

๐Ÿ˜ฒ   Execution: RoboCopy.exe "Z:\TestA\." "Z:\TestB\." *.gz /MOV /FP /NP /IS /Z /E /L

  • This is Z:\TestA and all subfolder/files listed before using Robocopy:
Folder PATH listing
Volume serial number is 32E8-70C5
Z:\TESTA
+---Dir 00
|   |   File 00 in Dir 00.gz
|   |   File 00 in Dir 00.keep
|   |   File 01 in Dir 00.gz
|   |   File 01 in Dir 00.keep
|   |   File 02 in Dir 00.gz
|   |   File 02 in Dir 00.keep
|   |
|   +---Sub 00
|   |       File 00 in Sub 00.gz
|   |       File 01 in Sub 00.gz
|   |       File 02 in Sub 00.gz
|   |
|   +---Sub 01
|   |       File 00 in Sub 01.gz
|   |       File 00 in Sub 01.keep
|   |       File 01 in Sub 01.gz
|   |       File 01 in Sub 01.keep
|   |       File 02 in Sub 01.gz
|   |       File 02 in Sub 01.keep
|   |
|   \---Sub 02
|           File 00 in Sub 02.gz
|           File 00 in Sub 02.keep
|           File 01 in Sub 02.gz
|           File 01 in Sub 02.keep
|           File 02 in Sub 02.gz
|           File 02 in Sub 02.keep
|
+---Dir 01
|   |   File 00 in Dir 01.gz
|   |   File 00 in Dir 01.keep
|   |   File 01 in Dir 01.gz
|   |   File 01 in Dir 01.keep
|   |   File 02 in Dir 01.gz
|   |   File 02 in Dir 01.keep
|   |
|   \---Sub 00
|       |   File 00 in Sub 00.gz
|       |   File 00 in Sub 00.keep
|       |   File 01 in Sub 00.gz
|       |   File 01 in Sub 00.keep
|       |   File 02 in Sub 00.gz
|       |   File 02 in Sub 00.keep
|       |
|       \---Sub Empty
+---Dir 02
|       File 00 in Dir 02.gz
|       File 00 in Dir 02.keep
|       File 01 in Dir 02.gz
|       File 01 in Dir 02.keep
|       File 02 in Dir 02.gz
|       File 02 in Dir 02.keep
|
\---Dir Empty

Note: Output from command-line :\>tree /f /a Z:
  • This is Z:\TestA and all subfolder/files listed after using Robocopy:
Folder PATH listing
Volume serial number is 32E8-70C5
Z:\TESTA
+---Dir 00
|   |   File 00 in Dir 00.keep
|   |   File 01 in Dir 00.keep
|   |   File 02 in Dir 00.keep
|   |   
|   +---Sub 00
|   +---Sub 01
|   |       File 00 in Sub 01.keep
|   |       File 01 in Sub 01.keep
|   |       File 02 in Sub 01.keep
|   |       
|   \---Sub 02
|           File 00 in Sub 02.keep
|           File 01 in Sub 02.keep
|           File 02 in Sub 02.keep
|           
+---Dir 01
|   |   File 00 in Dir 01.keep
|   |   File 01 in Dir 01.keep
|   |   File 02 in Dir 01.keep
|   |   
|   \---Sub 00
|       |   File 00 in Sub 00.keep
|       |   File 01 in Sub 00.keep
|       |   File 02 in Sub 00.keep
|       |   
|       \---Sub Empty
+---Dir 02
|       File 00 in Dir 02.keep
|       File 01 in Dir 02.keep
|       File 02 in Dir 02.keep
|       
\---Dir Empty
  • This is my New Folder Z:\TestB and all subfolder/files listed after using Robocopy:
Folder PATH listing
Volume serial number is 32E8-70C5
Z:\TESTB
+---Dir 00
|   |   File 00 in Dir 00.gz
|   |   File 01 in Dir 00.gz
|   |   File 02 in Dir 00.gz
|   |   
|   +---Sub 00
|   |       File 00 in Sub 00.gz
|   |       File 01 in Sub 00.gz
|   |       File 02 in Sub 00.gz
|   |       
|   +---Sub 01
|   |       File 00 in Sub 01.gz
|   |       File 01 in Sub 01.gz
|   |       File 02 in Sub 01.gz
|   |       
|   \---Sub 02
|           File 00 in Sub 02.gz
|           File 01 in Sub 02.gz
|           File 02 in Sub 02.gz
|           
+---Dir 01
|   |   File 00 in Dir 01.gz
|   |   File 01 in Dir 01.gz
|   |   File 02 in Dir 01.gz
|   |   
|   \---Sub 00
|       |   File 00 in Sub 00.gz
|       |   File 01 in Sub 00.gz
|       |   File 02 in Sub 00.gz
|       |   
|       \---Sub Empty
+---Dir 02
|       File 00 in Dir 02.gz
|       File 01 in Dir 02.gz
|       File 02 in Dir 02.gz
|       
\---Dir Empty

  • You can get the information of the executions that RoboCopy will give you just by using switch /L:
โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“  
   ROBOCOPY     ::     Robust File Copy for Windows                              
โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“  
                                                                                 
  Started : Monday, July 20, 2020 1:34:01 PM                                     
   Source : z:\TestA                                                             
     Dest : z:\TestB\                                                            
                                                                                 
    Files : *.gz                                                                 
                                                                                 
  Options : /FP /L /S /E /DCOPY:DA /COPY:DAT /MOV /Z /IS /R:1000000 /W:30        
                                                                                 
โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“   
                                                                                 
      New Dir          0    z:\TestA\                                            
      New Dir          3    z:\TestA\Dir 00\                                     
        New File             3.9 m    z:\TestA\Dir 00\File 00 in Dir 00.gz       
        New File             3.9 m    z:\TestA\Dir 00\File 01 in Dir 00.gz       
        New File             3.9 m    z:\TestA\Dir 00\File 02 in Dir 00.gz       
      New Dir          3    z:\TestA\Dir 00\Sub 00\                              
        New File             3.9 m    z:\TestA\Dir 00\Sub 00\File 00 in Sub 00.gz
        New File             3.9 m    z:\TestA\Dir 00\Sub 00\File 01 in Sub 00.gz
        New File             3.9 m    z:\TestA\Dir 00\Sub 00\File 02 in Sub 00.gz
      New Dir          3    z:\TestA\Dir 00\Sub 01\                              
        New File             3.9 m    z:\TestA\Dir 00\Sub 01\File 00 in Sub 01.gz
        New File             3.9 m    z:\TestA\Dir 00\Sub 01\File 01 in Sub 01.gz
        New File             3.9 m    z:\TestA\Dir 00\Sub 01\File 02 in Sub 01.gz
      New Dir          3    z:\TestA\Dir 00\Sub 02\                              
        New File             3.9 m    z:\TestA\Dir 00\Sub 02\File 00 in Sub 02.gz
        New File             3.9 m    z:\TestA\Dir 00\Sub 02\File 01 in Sub 02.gz
        New File             3.9 m    z:\TestA\Dir 00\Sub 02\File 02 in Sub 02.gz
      New Dir          3    z:\TestA\Dir 01\                                     
        New File             3.9 m    z:\TestA\Dir 01\File 00 in Dir 01.gz       
        New File             3.9 m    z:\TestA\Dir 01\File 01 in Dir 01.gz       
        New File             3.9 m    z:\TestA\Dir 01\File 02 in Dir 01.gz       
      New Dir          3    z:\TestA\Dir 01\Sub 00\                              
        New File             3.9 m    z:\TestA\Dir 01\Sub 00\File 00 in Sub 00.gz
        New File             3.9 m    z:\TestA\Dir 01\Sub 00\File 01 in Sub 00.gz
        New File             3.9 m    z:\TestA\Dir 01\Sub 00\File 02 in Sub 00.gz
      New Dir          0    z:\TestA\Dir 01\Sub 00\Sub Empty\                    
      New Dir          3    z:\TestA\Dir 02\                                     
        New File             3.9 m    z:\TestA\Dir 02\File 00 in Dir 02.gz       
        New File             3.9 m    z:\TestA\Dir 02\File 01 in Dir 02.gz       
        New File             3.9 m    z:\TestA\Dir 02\File 02 in Dir 02.gz       
      New Dir          0    z:\TestA\Dir Empty\                                  
                                                                                 
โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“โ€“   
                                                                                 
               Total    Copied   Skipped  Mismatch    FAILED    Extras           
    Dirs :        10        10         0         0         0         0           
   Files :        21        21         0         0         0         0           
   Bytes :   82.66 m   82.66 m         0         0         0         0           
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00           
   Ended : Monday, July 20, 2020 1:34:01 PM                                      
                                                                                 

  • Obs.: If your folder Z:\TestB tree does not exist, RoboCopy will create: /E
  • RoboCopy.exe "Z:\TestA\." "Z:\TestB\." *.gz /MOV /FP /NP /IS /Z /E /L 

-------------------------------------------------------------------------------  
   ROBOCOPY     ::     Robust File Copy for Windows                              
-------------------------------------------------------------------------------  
                                                                                 
  Started : Monday, July 20, 2020 11:49:28 AM                                    
              Usage :: ROBOCOPY source destination [file [file]...] [options]    
                                                                                 
             source :: Source Directory (drive:\path or \\server\share\path).    
        destination :: Destination Dir  (drive:\path or \\server\share\path).    
               file :: File(s) to copy  (names/wildcards: default is "*.*").     
                                                                                 
::                                                                               
:: Copy options | used for move your files | :                                                                
::                                                                               
               /MOV :: MOVe files (delete from source after copying)             
                /FP :: include Full Pathname of files in the output.             
                /IS :: Include Same files.                                       
                /NP :: No Progress - don't display percentage copied.            
                 /Z :: copy files in restartable mode.                           
                 /E :: copy subdirectories, including Empty ones.                
                 /L :: List only - don't copy, timestamp or delete any files.

  • The Command-line:
RoboCopy.exe "Z:\TestA\." "Z:\TestB\." *.gz /MOV /FP /NP /IS /Z /E /L
  • The Outputs Results:
-------------------------------------------------------------------------------            
   ROBOCOPY     ::     Robust File Copy for Windows                                        
-------------------------------------------------------------------------------            
                                                                                           
  Started : Monday, July 20, 2020 2:31:40 PM                                               
   Source : Z:\TestA\                                                                      
     Dest : Z:\TestB\                                                                      
                                                                                           
    Files : *.gz                                                                           
                                                                                           
  Options : /FP /S /E /DCOPY:DA /COPY:DAT /MOV /Z /NP /IS /R:1000000 /W:30                  
                                                                                           
------------------------------------------------------------------------------             
                                                                                           
          New Dir          0    Z:\TestA\                                                  
          New Dir          3    Z:\TestA\Dir 00\                                           
            New File               3.9 m        Z:\TestA\Dir 00\File 00 in Dir 00.gz       
            New File               3.9 m        Z:\TestA\Dir 00\File 01 in Dir 00.gz       
            New File               3.9 m        Z:\TestA\Dir 00\File 02 in Dir 00.gz       
          New Dir          3    Z:\TestA\Dir 00\Sub 00\                                    
            New File               3.9 m        Z:\TestA\Dir 00\Sub 00\File 00 in Sub 00.gz
            New File               3.9 m        Z:\TestA\Dir 00\Sub 00\File 01 in Sub 00.gz
            New File               3.9 m        Z:\TestA\Dir 00\Sub 00\File 02 in Sub 00.gz
          New Dir          3    Z:\TestA\Dir 00\Sub 01\                                    
            New File               3.9 m        Z:\TestA\Dir 00\Sub 01\File 00 in Sub 01.gz
            New File               3.9 m        Z:\TestA\Dir 00\Sub 01\File 01 in Sub 01.gz
            New File               3.9 m        Z:\TestA\Dir 00\Sub 01\File 02 in Sub 01.gz
          New Dir          3    Z:\TestA\Dir 00\Sub 02\                                    
            New File               3.9 m        Z:\TestA\Dir 00\Sub 02\File 00 in Sub 02.gz
            New File               3.9 m        Z:\TestA\Dir 00\Sub 02\File 01 in Sub 02.gz
            New File               3.9 m        Z:\TestA\Dir 00\Sub 02\File 02 in Sub 02.gz
          New Dir          3    Z:\TestA\Dir 01\                                           
            New File               3.9 m        Z:\TestA\Dir 01\File 00 in Dir 01.gz       
            New File               3.9 m        Z:\TestA\Dir 01\File 01 in Dir 01.gz       
            New File               3.9 m        Z:\TestA\Dir 01\File 02 in Dir 01.gz       
          New Dir          3    Z:\TestA\Dir 01\Sub 00\                                    
            New File               3.9 m        Z:\TestA\Dir 01\Sub 00\File 00 in Sub 00.gz
            New File               3.9 m        Z:\TestA\Dir 01\Sub 00\File 01 in Sub 00.gz
            New File               3.9 m        Z:\TestA\Dir 01\Sub 00\File 02 in Sub 00.gz
          New Dir          0    Z:\TestA\Dir 01\Sub 00\Sub Empty\                          
          New Dir          3    Z:\TestA\Dir 02\                                           
            New File               3.9 m        Z:\TestA\Dir 02\File 00 in Dir 02.gz       
            New File               3.9 m        Z:\TestA\Dir 02\File 01 in Dir 02.gz       
            New File               3.9 m        Z:\TestA\Dir 02\File 02 in Dir 02.gz       
          New Dir          0    Z:\TestA\Dir Empty\                                        
                                                                                           
------------------------------------------------------------------------------             
                                                                                           
               Total    Copied   Skipped  Mismatch    FAILED    Extras                     
    Dirs :        10        10         0         0         0         0                     
   Files :        21        21         0         0         0         0                     
   Bytes :   82.66 m   82.66 m         0         0         0         0                     
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00                     
                                                                                           
                                                                                           
   Speed :           952499538 Bytes\sec.                                                  
   Speed :           54502.460 MegaBytes\min.                                              
   Ended : Monday, July 20, 2020 2:31:41 PM 

  • About the /R: and /W: that automatically appear in the Robocopy summary:
 /R:n :: number of Retries
 /W:n :: Wait time between retries

enter image description here

enter image description here

  • Some further reading:

    [โˆš] RoboCopy

    [โˆš] RoboCopy.doc (|Google/doc|)

Solution 2:

Use xcopy with the /s switch (i.e. copy subdirectories and files).

For example, the following copies all files with .gz extension and their directory tree from TestA to testB.

xcopy "c:\TestA\*.gz" "c:\TestB\" /s

It will copy all folders, as well as their contents, if they have .gz files. If you want to copy the full directory tree, even folders without .gz files, use the /t switch.