views:

234

answers:

3

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?

+3  A: 

To copy ignoring empty dirs you can use one of:

robocopy c:\source\ c:\dest\ * /s
xcopy c:\source c:\dest\*.* /s
Alex K.
+2  A: 

xcopy's /s will ignore blank folder when copying

xcopy * path\to\newfolder /s /q
S.Mark
Why use external dir command when FOR has directory support with /D and /R ?
Anders
Thanks for pointing out @Anders, its just my knowledge about batch commands are limited, +1ed to yours. I've learnt something more now. Thanks.
S.Mark
@S.Mark: Now that I have looked at it a bit more, your FOR code has several issues: 1)Spaces in paths. 2)Are you sure DIR will print the deepest folder first? If not, c:\folder1\folder2 will fail if folder2 is empty and folder1 only has folder2.
Anders
@Anders, yeah, thats why I noted it is not recursive. DIR does not print deepest folder first with above usage. To remove folder1, script need to run 2 times. anyway, I'm going **remove** `for` part now. thanks again.
S.Mark
+2  A: 
@echo off
setlocal ENABLEEXTENSIONS
call :rmemptydirs "%~1"
goto:EOF
:rmemptydirs
FOR /D %%A IN ("%~1\*") DO (
    REM recurse into subfolders first...
 call :rmemptydirs "%%~fA"
)
RD "%~f1" >nul 2>&1
goto:EOF

Call with: rmemptydirs.cmd "c:\root dir to delete empty folders in"

Anders