6

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.

4

2 に答える 2

7

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.

于 2013-03-25T14:38:26.740 に答える
1

Robocopy /S only will do the trick, I have noticed that when you run command if you look at the destination folder before it's completed you might see some empty folder but they will be gone once the process is completed.

于 2015-07-14T11:50:33.810 に答える