Assistance please with batch file to delete empty folders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dobbies
Posts: 8
Joined: 30 Jan 2013 01:29

Assistance please with batch file to delete empty folders

#1 Post by dobbies » 14 Feb 2013 04:29

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Assistance please with batch file to delete empty folder

#2 Post by foxidrive » 14 Feb 2013 04:37

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

dobbies
Posts: 8
Joined: 30 Jan 2013 01:29

Re: Assistance please with batch file to delete empty folder

#3 Post by dobbies » 14 Feb 2013 13:57

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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Assistance please with batch file to delete empty folder

#4 Post by Squashman » 14 Feb 2013 14:14

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

dobbies
Posts: 8
Joined: 30 Jan 2013 01:29

Re: Assistance please with batch file to delete empty folder

#5 Post by dobbies » 15 Feb 2013 02:30

Much appreciated

Thanks

Post Reply