How to delete empty folders using windows command line?

Do I need to delete all empty folders from my application folder using the windows command prompt?

How can I create a bat file like this?

Please help me.

+53
command-line windows command folder
Oct 20 2018-11-11T00:
source share
11 answers
for /f "usebackq" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d" 

from: http://blogs.msdn.com/b/oldnewthing/archive/2008/04/17/8399914.aspx

Of course, d will check it first without deleting it before I do this command. In addition, the comment version has been changed here, which contains folders with spaces:

  for /f "usebackq delims=" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d" 

PS there are more blog comments that may help you, so be sure to read them before you try this.

+43
Oct 20 2018-11-11T00:
source share

You can use ROBOCOPY. It is very simple and can also be used to delete empty folders within a large hierarchy.

 ROBOCOPY folder1 folder1 /S /MOVE 

Here are both the source and destination of folder1 , since you only need to delete the empty folders, and not move the other (required) files to another folder. /S option - skip copying (moving - in the above case) of empty folders. It is also faster since files move inside the same drive.

+74
May 09 '15 at 10:44
source share

The easiest way is to make xcopy to make a copy of the entire directory structure using the / s switch. help for / s says Copies of directories and subdirectories except empty.

 xcopy dirA dirB /S 

where dirA is the source with empty folders. DirB will be a copy without empty folders

+52
07 Feb '13 at 3:03
source share

You do not need to use backq:

 FOR /F delims^= %%A IN ('DIR/AD/B/S^|SORT/R') DO RD "%%A" 
+13
Apr 11 '13 at 16:51
source share

Adding to the corrupt answer from the same link page is the PowerShell version http://blogs.msdn.com/b/oldnewthing/archive/2008/04/17/8399914.aspx#8408736

Get-ChildItem -Recurse . | where { $_.PSISContainer -and @( $_ | Get-ChildItem ).Count -eq 0 } | Remove-Item

or, more briefly,

gci -R . | where { $_.PSISContainer -and @( $_ | gci ).Count -eq 0 } | ri

credit is sent to the author of the message

+6
Oct 08 '13 at 5:11
source share

from the command line: for / R / D% 1 in (*) do rd "% 1"

in the batch file for / R / D %% 1 in (*) do rd "%% 1"

I donโ€™t know if it is registered as such, but it works in W2K, XP and Win 7. And I donโ€™t know if it will always work, but it will never delete files by accident.

+3
Oct 04 '15 at 19:22
source share

This is a bird of the above. It deletes ALL files older than X days and deletes all empty folders for a given path. To simply set the days, folder path and drive

 @echo off SETLOCAL set days=30 set folderpath=E:\TEST\ set drive=E: ::Delete files forfiles -p %folderpath% -s -d -%days% -c "cmd /c del /q @path " ::Delete folders cd %folderpath% %drive% for /f "usebackq delims=" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d"` 
+2
May 27 '15 at 11:02
source share

Install any UNIX interpreter for windows (Cygwin or Git Bash) and run cmd:

find / path / to / directory -empty -type d

 To find them 

find / path / to / directory -empty -type d -delete

 To delete them 

(not actually using the Windows cmd prompt, but it is easy and it took a few seconds to start)

0
Jun 02 '16 at 6:05
source share
 @echo off set /p "ipa= ENTER FOLDER NAME TO DELETE> " set ipad="%ipa%" IF not EXIST %ipad% GOTO notfound IF EXIST %ipad% GOTO found :found echo DONOT CLOSE THIS WINDOW md ccooppyy xcopy %ipad%\*.* ccooppyy /s > NUL rd %ipad% /s /q ren ccooppyy %ipad% cls echo SUCCESS, PRESS ANY KEY TO EXIT pause > NUL exit :notfound echo I COULDN'T FIND THE FOLDER %ipad% pause exit 
0
Mar 15 '17 at 13:01
source share

Everything will be fine. This is the best way to delete old files and recursively delete empty directories. the following .bat file,

 forfiles /p [PATH] /s /m [FILE-PATTERN] /d -[DAYS] /c "cmd /c del @path" for /f "delims=" %%d in ('dir [PATH] /s /b /ad ^| sort /r') do rd "%%d" 

Placeholders should be replaced as follows (without quotes):

 [DAYS] = Max. age of the files in days, eg "10" [PATH] = Path to search for old files and empty folders, eg "C:\Backup\" [FILE-PATTERN] = Pattern that matches files to delete, eg "*.bkp" 

The script has been successfully tested on Windows 7 and Windows Server 2003.

0
Oct 07 '17 at 6:20
source share

Well, just another suggestion (for a simple 1-level directory structure with no spaces), I found it useful (at some point from http://www.pcreview.co.uk/forums/can-check-if-folder-empty -bat-file-t1468868.html )

 for /f %a in ('dir /ad/b') do (if exist %a\* echo %a not Empty) 

or

 for /f %a in ('dir /ad/b') do (if not exist %a\* echo %a Empty) 

therefore removal will be:

 for /f %a in ('dir /ad/b') do (if not exist %a\* rmdir %a) 
-one
Nov 07 '13 at 15:04
source share



All Articles