I have a directory on my Windows 7 machine that has hundreds if not thousands of sub-directories. Some of them have files, some do not. I want to delete all the empty directories.
Looking at the del and rmdir DOS command, it does not look like you can recursively do this without deleting all the files. Is there a way to do this from the command line?
412 Answers
You can use Remove Empty Directories utility.
Alternatively you can use this one-liner batch file (from DownloadSquad):
for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do rd "%d"(if used inside a batch file, replace %d with %%d)
This works because rd will not remove a directory that contains files.
You can also use ROBOCOPY. It is very simple and can also be used to delete empty folders inside large hierarchy.
ROBOCOPY folder1 folder1 /S /MOVEHere both source and destination are folder1, as you only need to delete empty folders, instead of moving other files to different folder. /S option is to skip copying(moving, in the above case) empty folders. It is also faster as the files are moved inside the same drive.
Since Cygwin comes with GNU find, you can do this:
find . -type d -empty -deleteOr to avoid the noise when a folder no longer exists:
find . -type d -empty -execdir rmdir {} + 3 The free utility EmptyFolderNuker does this fine, from a base folder of your choice. It also removes those directories only containing empty sub-directories.
5Hmmm... maybe even simpler solution:
for /d /r %d in (*.*) do rd "%d"
Start this from the folder you want empty folders to be deleted.
/d - will work on folders, not files /r - will recurse subdirs
1If you have Cygwin installed, you could do this:
find -type d -exec rmdir {} \; 3 If you're working in emacs (making this platform-agnostic), the following works:
(defun *-delete-empty-directories (root-directory) "Recursively delete empty directories in ROOT-DIRECTORY.
When called from dired, `dired-current-directory' is used for
ROOT-DIRECTORY." ;; Interface (interactive (list (if (eq major-mode 'dired-mode) (expand-file-name (dired-current-directory)) (read-from-minibuffer "Root directory: ")))) (when (or (null root-directory) (string= "" root-directory)) (user-error "No root directory provided")) (when (called-interactively-p 'interactive) (unless (yes-or-no-p (format "Delete all non-empty directories in `%s'? " root-directory)) (user-error "Directory `%s' has been left untouched" root-directory))) ;; Implementation (require 'f) (let ((entries (f-directories root-directory))) (while entries (let ((curdir (car entries))) (when (f-directories curdir) (*-delete-empty-directories curdir)) (unless (f-entries curdir) (delete-directory curdir) (message "Directory deleted: `%s'" curdir)) (setq entries (cdr entries))))) 6 Combining Gareth's and G-Man's posts:
find . -depth -type d -empty -execdir rmdir {} +Edit: But that gave a security error because of 'C' in my PATH var...so instead:
$find . -depth -type d -empty | while read dir; do (rmdir -v $dir); doneI don't use xargs because it appears to have an input line limit (of about 1024 lines, I think?), whereas
while read x; do (command $x); donejust keeps on going for as long as it has input. Leave out the '-v' verbose flag if you don't want to see the results and/or want it to run faster.
None of the previous answers worked for me, so I made the following file:
EmptyDirectoriesRemove.cmdContents:
@setlocal enableextensions enabledelayedexpansion
:walk_tree
for /D %%d in (*) do (
cd %%d
@CALL :walk_tree %%d
cd ..
rd %%d
)
endlocalUsage: Cd to the top level directory you want to clean up. From the command line prompt, run:
EmptyDirectoriesRemove.cmd
Warnings will show up for non-empty directories.
Usual disclaimers: Use at your risk. Backup before testing. etc.
The 4NT shell (nowadays Take Command) has a /sx option to "DEL". /S is recursive, the appended X is remove empty dirs.
You can use Powershell:
Get-ChildItem "Path" -recurse | Where-Object {($_.PSIsContainer -eq $true) -and ((gci $_.fullName).count -eq 0)} | Remove-Item -Force You can use vbscript to do this:
' Save in deleteEmptyFolders.vbs
' call with:
' cscript deleteEmptyFolders.vbs C:\RootFolder
If WScript.Arguments.Count > 0 Then directory = WScript.Arguments.Item(0) WSH.Echo("Deleting empty folders starting from " & directory)
Else WSH.Echo "Please supply a directory as an argument" WSH.Quit
End If
Set ofso = CreateObject("Scripting.FileSystemObject")
Set rootFolder = ofso.GetFolder(directory)
Sub DeleteEmptySubfoldersIn(folder) For Each subfolder in folder.SubFolders On Error Resume Next '' VBScript TRY ' The script can error out if you don't have permissions to delete ' the subfolder DeleteEmptySubfoldersIn subfolder Next ''WScript.Echo folder.Path If folder.Files.Count = 0 And folder.SubFolders.Count = 0 Then 'TEST: print what you'll delete WScript.Echo "Was going to delete " & folder.Path & " b/c it's EMPTY" 'DISARMED uncomment line below to make it work '''''ofso.DeleteFolder folder End If
End Sub
DeleteEmptySubfoldersIn rootFolder