How to prevent Robocopy coping an empty top level directory?

batch-file, robocopy

Solution

If you use the `robocopy /s` command without the `/e` switch, then it won't copy any empty folders. It appears that you are using `robocopy /s /e` since you are having problems with empty top-level folders. If you don't want any empty sub-folders either, then just omit the `/e` switch and no empty folders will be created in the destination. If you need to copy the empty sub-folders, then you can't avoid also copying the empty top-level folders. However, you could easily remove the empty top level folders like this:

robocopy C:\Source\Dir X:\Dest\Dir /s /e
cd /d X:\Dest\Dir
for /d %%i in (*.*) do rd "%%i" > nul 2>&1

The `cd` command changes to the destination drive & folder and then the `for` command tries to remove all top-level folders. Only the ones that are empty will be removed. The `> nul 2>&1` hides the output and errors since folders that aren't empty will show an error message, which can just be ignored.

Problem

Is there a way to utilize Robocopy so that if it is called against an empty top level directory it does not copy it? I am using Robocopy to identify files and directories that are then zipped up within a script - empty directories cannot be zipped up using the standard Windows archiving features. I am aware of the /s switch which prevents empty sub directories from being copied.

Original source