Using the batch to quickly delete the folder

  

Someone asked me, what tools can find and delete all empty folders on the hard disk, or folders containing only empty subfolders? Killing a chicken with a slaughter knife, in fact, without any software, directly using the batch command can achieve this purpose.
Step 1: Enter the MS-DOS window of Windows (Windows 2000/XP called the command line prompt), enter the drive to find the empty folder, execute:
dir /ad/b/s |  Sort /r >empties.bat.
Tips
above the command line "| The input method is to hold down the Shift key and press the "\\" key.

Step 2: After the command is run, an Empties.bat file will be generated in the current directory, which contains all the folders in reverse order. List. Open the file with Word or Notepad, put each line of content into a pair of English quotation marks, and then insert the "rd" prefix in front of each line (add a space after rd).
Take Word as an example Press Ctrl+H to open the "Find and Replace" function and find "^p" (without quotes, the same below. "^p" can be entered using the following method: Click the "Advanced" button in the "Find and Replace" dialog box , then click "special characters → paragraph mark"), replace with "^prd" (rd followed by a space), and then manually correct the contents of the first line and the last line. Save the modified Empties.bat file, enter the command line The window executes this batch file. Empties.bat will attempt to delete (ie the rd command) each folder, but the folder containing the content cannot be deleted with the rd command, and all empty folders are deleted as a result.
Know why: Let's take a look at the operation of the previous Dir command: "/ad" requires the Dir command to select all directories; "/b" means that only the folder name is displayed (the title information or summary of the Dir command is not displayed); "/s" means search, which is to find the current folder and all its subfolders. The output of the Dir command is via the pipe command ("| ") is passed to the Sort command. The Sort command is used for sorting, and the "/r" option is for reverse ordering.
Finally, the output of the Sort command is redirected (">") to the Empties.bat file. The command arranges the output in reverse order, so the subfolders in the Empties.bat file are always in front of the parent folder, so when the folder is deleted, the empty subfolders are always deleted first, and the empty parent folder is also deleted. As you can see, clever use of Windows built-in commands, many seemingly complex tasks can be easily accomplished!

Tips
Three small tricks for Dir
★View folder All hidden files: If you want to view all the hidden files in the Windows folder and all its subfolders, just type: DIR C:\\Windows.* /AH/S/B/P.
★Easily create long files Name file list: You can create a batch file such as Getlist.bat, where the content is only DIR *.* /B > LIST.TXT, copy it to the folder where you want to create a file list, and then double-click to run Folder all the columns of the file List.txt saved to a file
★ appeared together multiple directories: If you want to simultaneously list the contents of the C drive and the D root directory, just type: DIR C: D: can be.

Copyright © Windows knowledge All Rights Reserved