Implementation code:
Copy Code code as follows:
@echo off
for/f "tokens=*"%%a in (' dir/b/ad/s "destination folder" ^|SORT/R ") do rd"%%a "2>nul
Pause
Goto:eof
@echo off
for/f "delims="%%i in (' dir/ad/b/s ') do Call:rd_dir_empty "%%i"
Goto:eof
REM ======= Delete Empty directory =========
: Rd_dir_empty
Rd%1 2>nul| | Goto:eof
Set dir_route=%1
for/f "delims="%%i in (%dir_route%) do (
Set dir_route= "%%~dpi"
for/f "delims="%%j in (' dir/ad/b "%%~dpi" ') do rd "%%~dpi%%j" 2>nul| | Goto:eof
)
:: The end of the path to remove, so that the set route_deepest= "%%~dpi" can take to the previous level of the path
If "%dir_route:~-2,1%" = = "\" Set dir_route= "%dir_route:~1,-2%"
if/i not "%cd%" ==%dir_route% call:rd_dir_empty%dir_route%
Goto:eof
Step One: Enter the Windows MS-DOS window (Windows 2000/XP is called a command-line prompt) and go to the drive where you want to find an empty folder, and execute:
dir/ad/b/s | SORT/R >empties.bat.
Small Tips
Copy Code code as follows:
"|" in the command line above The input method is to hold down the SHIFT key and press the "\" key.
Step two: After the command runs, a empties.bat file is generated in the current directory that contains a list of all the folders in reverse order. Open the file in Word or Notepad, place each line within a pair of English quotes, and then insert the "rd" prefix in front of each line (plus a space after Rd).
Take word for example, press Ctrl+h to open the Find and Replace feature, and look for "^p" (without quotes, below). "^p" can be entered by using the "Advanced" button in the Find and Replace dialog box, then clicking special character → paragraph mark), substituting "^PRD" (with a space after Rd), and then manually correcting the contents of the first and last line. Save the modified Empties.bat file and go to the command Line window to execute the batch file. Empties.bat will attempt to delete each folder (that is, the RD command), 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 how the previous dir command works: "/ad" requires the dir command to select all directories; /b "indicates that only the folder name is displayed (the header information or summary of the DIR command is not displayed);" /s represents the search, which is to find the current folder and all its subfolders. The output of the dir command is through a pipe command ("|") Passed to the sort command. The sort command is sorted, and the "/r" option indicates a reverse order.
Finally, the output of the sort command is redirected (">") to the Empties.bat file. Because the sort command is required to arrange the output in reverse order, the Empties.bat file is always in front of the parent folder, so when the folder is deleted, the empty subfolder is always deleted, and the empty parent folder is deleted. As you can see, with the clever use of Windows built-in commands, many seemingly complex tasks can be done easily!
Tip: Dir's Three Little tricks
★ View folder all hidden files: If you want to see all the hidden files in the Windows folder and all of its subfolders, simply type: DIR c:\windows.*/ah/s/b/p.
★ Easily create a long file name file list: You can create a batch file such as Getlist.bat, where the contents are only dir *.*/b >list. TXT, copy it to the folder where you want to create a list of files, and then double-click to save the list of all files in the folder to the List.txt file.
★ Multiple Catalogues: If you want to list the contents of both C-disk and D-packing directory, just type: DIR c:d: You can. [