Robocopy /S creates empty folders

8,880

Solution 1

Here's what I ended up doing (replace Public with your username):

@echo off
@rem - create decent date and filename strings
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c%%a%%b)
for /f "tokens=1-2 delims=: " %%a in ('time /t') do (set mytime=%%a%%b)
set fname=E:\%mydate%_%mytime%_RClog_d.txt

@rem - copy newer files to a new timestamped tree
robocopy C:\Users\Public\ E:\%mydate%_Users_C\Public\ /XJD /ZB /S /COPY:DATSO /R:3 /W:1 /MAXAGE:7 /NC /NDL /FP /LOG:%fname% /NP 

@rem - and this removes the empty folders created by the command above
robocopy E:\%mydate%_Users_C\Public\ E:\%mydate%_Users_C\Public\ /S /MOVE /NDL /LOG:NUL /NP 

And this is what shows up on disk:

06/04/2018  01:00 PM         9,644,168 20180604_0241_RClog.txt
06/10/2018  02:34 AM           563,342 20180610_0230_RClog_d.txt
06/10/2018  02:30 AM    <DIR>          20180610_Users_C
06/10/2018  02:30 AM    <DIR>          20180610_Users_D
06/17/2018  02:30 AM           394,755 20180617_0230_RClog_d.txt
06/17/2018  02:30 AM    <DIR>          20180617_Users_C
06/17/2018  02:30 AM    <DIR>          20180617_Users_D
06/24/2018  02:30 AM           833,475 20180624_0230_RClog_d.txt
06/24/2018  02:30 AM    <DIR>          20180624_Users_C
06/24/2018  02:30 AM    <DIR>          20180624_Users_D
07/01/2018  02:31 AM           891,884 20180701_0230_RClog_d.txt
07/01/2018  02:30 AM    <DIR>          20180701_Users_C
07/01/2018  02:31 AM    <DIR>          20180701_Users_D
09/17/2017  01:09 AM    <DIR>          Users_C
06/04/2018  12:32 PM    <DIR>          Users_D

Solution 2

A few things. . .

  1. It appears that you will need to use the /MIR parameter in place of the /S parameter to ensure directories are not copied over to the destination once they already exist there.
    • Using the /MIR parameter will mean empty directories will be copied over to the destination location so this is the trade-off to ensure the already existing directories are not copied over again per each run. There is a simple workaround to quickly remove the empty directories in the destination location which entails running Robocopy <dest> <dest> /S /MOVE directly after the command using the /MIR parameter is run.
  2. If you want omit directories from the Robocopy output then simply add the /NDL parameter.

Robocopy Script

Robocopy "<Source>" "<Destination>" *.* /MIR /COPY:DATSO /B /NP /NDL /XJ /REG /MAXAGE:1 /MT:16 /R:3 /W:1
Robocopy <dest> <dest> /S /MOVE 

Important Notes:

  • Using the /MIR switch will also mean that files and folder in the destination that no longer exist in the source will be purged from the destination

  • Copying over empty directories per script run versus copying over that same thousands of directories per script run to destination that already exist in destination seems like a small price to pay and since those can easily and quickly be removed this seems like a no-brainer

  • The Robocopy command of Robocopy <dest> <dest> /S /MOVE which deletes the empty directories from the destination uses the destination location for BOTH source and destination


Further Resources

  • Robocopy
  • Robocopy /?
    • /MIR :: MIRror a directory tree (equivalent to /E plus /PURGE).
      /MOVE :: MOVE files AND dirs (delete from source after copying).
      /NDL :: No Directory List - don't log directory names.
      
Share:
8,880

Related videos on Youtube

GWild
Author by

GWild

Updated on September 18, 2022

Comments

  • GWild
    GWild over 1 year

    I'm trying to stop robocopy from creating empty folders when it is copying. The SOURCE folders are not empty; but they also have no new files to copy. Yet robocopy creates them at the destination.

    My goal is to create a copy of files 1 day old. But I'd like to avoid seeing 11,674 new folders and entries in the log when only 4 files were copied... and not plug up the disk with another 12k files every day I do an incremental backup of only 4 to 50 files.

    I know fairly well how robocopy works ... and I am pretty sure this would be considered a 'feature' by most design teams rather than a 'defect', so asking MS to look into it is mostly pointless.

    A workaround is to use XCOPY as follows, but it requires calculating and formatting a legal date:

    E:\>xcopy C:\Users\Public\* E:\C_Users\Public\ /D:6-1-2018 /S /B /C /H /J /K /O /Y
    

    Any ideas or suggestions?

    Here's an example of the problem, note the created Adobe AppData hierarchy is all empty:

    -------------------------------------------------------------------------------
       ROBOCOPY     ::     Robust File Copy for Windows                              
    -------------------------------------------------------------------------------
    
      Started : Fri Jun 01 03:42:50 2018
    
       Source : C:\Users\Public\
        Dest : E:\20180601_Users_C\Public\
    
        Files : *.*
    
        Options : *.* /S /COPY:DATSO /B /NP /XJ /REG /MAXAGE:1 /MT:16 /R:3 /W:1 
    
          New Dir          2    C:\Users\Public\.idlerc\
          New Dir          0    C:\Users\Public\AppData\
          New Dir          4    C:\Users\Public\AppData\Local\
            New File            7603    Resmon.ResmonCfg     100%  
          New Dir          0    C:\Users\Public\AppData\Local\Adobe\
          New Dir          0    C:\Users\Public\AppData\Local\Adobe\AAMUpdater\
          New Dir          7    C:\Users\Public\AppData\Local\Adobe\AAMUpdater\1.0\
          New Dir          1    C:\Users\Public\AppData\Local\Adobe\AAMUpdater\1.0\Data\ 
    
        [...snip...]
    
        ------------------------------------------------------------------------------
    
                           Total    Copied   Skipped  Mismatch    FAILED    Extras
                Dirs :     11674     11674         0         0         0         0
               Files :    443686         4    443682         0         0         0
               Bytes :   1.789 t   53.26 m   1.789 t         0         0         0
               Times :   0:00:40   0:00:09                       0:00:00   0:00:05
    
               Ended : Fri Jun 01 03:43:09 2018
    ===========
    
  • GWild
    GWild almost 6 years
    The thing I'd like to happen, is when RC processes a folder with no new data, move on to the next. What it's doing is skipping any empty folders (/S), then copying every folder it comes to that isn't empty. This creates a full tree on the destination. And when this runs daily per user, 12k x N users are dumped to the backup array.
  • GWild
    GWild almost 6 years
    I will look at the second RC command to clean out empty folders ... that may work better than XCOPY, though XCOPY has an advantage of working somewhat better handling junctions. With RC I simply was ignoring them and what they pointed to.