Is there a way to delete all empty sub-directories below a given directory from a batch file?
Or is it possible to recursively copy a directory, but excluding any empty directories?
You really have two questions:
1. Is there a way to delete all empty sub-directories below a given directory from a batch file?
Yes. This one-line DOS batch file works for me. You can pass in an argument for a pattern / root or it will use the current directory.
for /f "delims=" %%d in ('dir /s /b /ad %1 ^| sort /r') do rd "%%d" 2>nul
The reason I use 'dir|sort' is for performance (both 'dir' and 'sort' are fairly fast). It avoids the recursive batch function solution used in one of the other answers which is perfectly valid but can be infuriatingly slow :-(
2. Or is it possible to recursively copy a directory, but excluding any empty directories?
There are a number of ways to do this listed in other answers.
To copy ignoring empty dirs you can use one of:
robocopy c:\source\ c:\dest\ * /s
xcopy c:\source c:\dest\*.* /s
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With