Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Robocopy coping an empty top level directory?

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.

like image 577
DevHouse Avatar asked Mar 25 '13 11:03

DevHouse


2 Answers

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.

like image 119
James L. Avatar answered Oct 05 '22 22:10

James L.


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.

like image 38
fr4nk Avatar answered Oct 05 '22 21:10

fr4nk