Page 1 of 1

Assistance please with batch file to delete empty folders

Posted: 14 Feb 2013 04:29
by dobbies
Hi

Could someone please offer me a batch file that will delete all empty folders and subfolders in a designated place.

The reason is that I have a backup program that is set to perform incremental backups every day but in doing so it copies every folder & subfolder on the destination drive even if it's empty.

There is a setting on the program to delete the empty folders after each backup but for some reason it doesn't always work.
Any help would be appreciated.

Thanks

Re: Assistance please with batch file to delete empty folder

Posted: 14 Feb 2013 04:37
by foxidrive
This is untested but should work.

Run it inside the folder tree you want to remove the empty folders from.


Code: Select all

@echo off
dir /ad /b /s |sort /r >tmp.tmp
for /f "delims=" %%a in (tmp.tmp) do rd "%%a" 2>nul
del tmp.tmp

Re: Assistance please with batch file to delete empty folder

Posted: 14 Feb 2013 13:57
by dobbies

Code: Select all

@echo off
dir /ad /b /s |sort /r >tmp.tmp
for /f "delims=" %%a in (tmp.tmp) do rd "%%a" 2>nul
del tmp.tmp


Thank you very much for your reply foxidrive, it works just fine run from the folder in question.

If I wanted to store and run this batch file from a central folder to delete empty folders on a network drive, say "F:\backup", could you tell me how to do that?

Many thanks

Re: Assistance please with batch file to delete empty folder

Posted: 14 Feb 2013 14:14
by Squashman
dobbies wrote:

Code: Select all

@echo off
dir /ad /b /s |sort /r >tmp.tmp
for /f "delims=" %%a in (tmp.tmp) do rd "%%a" 2>nul
del tmp.tmp


Thank you very much for your reply foxidrive, it works just fine run from the folder in question.

If I wanted to store and run this batch file from a central folder to delete empty folders on a network drive, say "F:\backup", could you tell me how to do that?

Many thanks


dir /ad /b /s F:\backup |sort /r >tmp.tmp

Re: Assistance please with batch file to delete empty folder

Posted: 15 Feb 2013 02:30
by dobbies
Much appreciated

Thanks