How to delete empty folders using windows command prompt?

Related searches

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

How can I create a bat file like that?

Please help me.

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 I'd test it first without deleting before I do that command. Also, here's a modded version from the comments that includes folders with spaces:

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

P.S. there are more comments in the blog post that might help you out so be sure to read those too before you try this out

How to delete empty folders using windows command prompt , 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. To delete files using Command Prompt, make sure to open it with admin privileges. 1] Delete the desired file When the CMD window opens, navigate to the folder where the file is available. You will

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

ROBOCOPY folder1 folder1 /S /MOVE

Here both source and destination are folder1, as you only need to delete empty folders, instead of moving other(required) 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.

Recursively delete empty directories in Windows, 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� How to Find and Remove Empty Folders in Windows? 1. Remove empty folders using the “Find Empty Files-n-Folders” utility. Find Empty Files-n-Folders is an excellent tool that can find and 2. Remove empty folders using “for” and “rd” commands. 3. Find and remove empty folders using Robocopy

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

xcopy dirA dirB /S

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

Delete Empty Folders Recursively Using CMD // metinsaylan, When it comes to automating IT-related tasks, batch scripting is the best choice. And if you are using Windows you have really good alternatives� 8. You need to right-click on any folder. 9. Press on the Delete option. · Using the Command Prompt. 1. Go to the Command Prompt menu. 2. You need to find the Command Prompt. 3. It will launch

You don't need usebackq:

FOR /F delims^= %%A IN ('DIR/AD/B/S^|SORT/R') DO RD "%%A"

Copy the path of the folder which you want to delete Run Command Prompt as Admin Type Command Prompt on Windows search box. Right-click on its icon and select Run as Administrator.

Adding to corroded answer from the same referenced page is a 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 tersely,

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

credit goes to the posting author

To delete a file or folder using the command prompt we must first navigate to the location of our file. By default the command line is located at a folder named after your computers username (C:\Users\MyPC). This folder contains your documents, downloads and desktop directories, among others.

To delete a folder using command prompt of windows, follow this procedure. Open a command prompt (Press the windows icon key and R to open the run window). Type cmd and hit enter from the keyboard. On a command prompt, enter the following command and hit enter from the keyboard.

Well, you can make use of an free tool for windows called Unlocker and it will help you to delete any undeletable files and folders from windows with ease. Advertisement Alternatively you can even use the command prompt on windows to delete files which are refusing to be deleted via the windows explorer.

Select the folder and click Scan Now. The tool will list empty files and folders in separate tabs. From the Empty Files tab, click Mark all Files and then click Delete Files. Similarly, to delete the 0-byte files in the selected folder tree, click on the Empty Files tab.

Comments
  • I like this one. The link here mentions that this one is easier (similar to the one below): for /f "tokens=*" %%d in ('dir /ad/b/s ^| sort /R') do rd "%%d"
  • Where do you put path to folders?
  • @MAGx2 I had this question too. I can't figure out how to post backquotes in the comments, but I'm using ([backquote]dir /ad/b/s "c:\path"[backquote])
  • If you don't use the line above in a batch file then you need to replace %%d with %d
  • It can be done easily using ROBOCOPY. See my answer below for details.
  • Love the robocopy solution. THANK YOU!
  • Very nice! +1 for the answer.
  • life saver answer.
  • OK, this seems great and works! I just don't understand why, if running the cmd from inside the project folder, its giving me "ERROR 32 (0x00000020) Deleting Source Directory" - meaning it can't delete the main directory! Thank god it can't delete it because is not supposed to - its not empty! ("The process cannot access the file because it is being used by another process." - where file must be the dir). I'm running inside it with: robocopy . . /s /move.
  • @PedroReis According to me, you are getting this error as you are trying to delete/move it while the child file/folder is still open. If you need more help, I think it is better to post it as a new question so that others who are facing this issue can find your question and resolve their issue.
  • This should be the accepted answer. Nice and simple! Thanks!