Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to delete only empty directories from a batch file [closed]

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?

like image 675
M4N Avatar asked Nov 29 '22 19:11

M4N


2 Answers

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.

like image 85
Adisak Avatar answered Dec 06 '22 09:12

Adisak


To copy ignoring empty dirs you can use one of:

robocopy c:\source\ c:\dest\ * /s
xcopy c:\source c:\dest\*.* /s
like image 35
Alex K. Avatar answered Dec 06 '22 10:12

Alex K.