Implementation Code :
Copy code The Code is as follows: @ echo off
For/F "tokens = *" % A in ('dir/B/AD/s "target 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 an 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
)
: Remove the \ at the end of the path to set route_deepest = "% ~ DPI "can be retrieved to the previous 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 1: Enter the Windows MS-DOS window (Windows 2000/XP is called a command line prompt), enter the drive to find the empty folder, and execute:
DIR/AD/B/S | sort/r> empties. bat.
Tips
Copy codeThe Code is as follows: the input method of "|" in the preceding command line is to press shift and press.
Step 2: After the command is run, an empties. BAT file is generated under the current directory, which contains the list of all folders in reverse order. Open the file in word or notepad, place each line of content in a pair of english quotes, and insert the "RD" prefix before each line (with a space after rd ).
Take word as an example. Press Ctrl + H to enable the "search and replace" function and find "^ P" (without quotation marks, the same below. "^ P" can be entered using the following method: In the "search and replace" dialog box, click the "advanced" button, and then click "special character> section mark "), replace it with "^ PRD" (add a space after rd), and then manually modify the content of the first and last lines. Save the modified empties. BAT file and run the batch file in the command line window. Empties. bat will try to delete every folder (that is, the role of the RD command), but folders containing content cannot be deleted using the RD command, and all empty folders are deleted.
Let's take a look at the running principle of the preceding dir command: "/ad" requires the Dir command to select all directories; "/B" indicates that only the folder name is displayed (the title or abstract of the Dir command is not displayed); "/s" indicates that the current folder and all its subfolders are searched. The dir command output is passed to the sort command through the pipeline command ("|. The sort command is used for sorting. The "/R" option indicates reverse sorting.
Finally, the output of the sort command is redirected (">") to the empties. BAT file. Empties. the sub-folders in the BAT file are always in front of the parent folder. Therefore, when deleting a folder, empty sub-folders are always deleted first, and empty parent folders are also deleted. As you can see, the clever use of Windows built-in commands can easily complete many seemingly complex tasks!
TIPS: three small tricks of Dir
★View All hidden files in a folder: to view all the hidden files in the Windows folder and all its subfolders, enter dir c: \ windows. */AH/S/B/P.
★Easily create a long file name list: You can create a batch file, such as getlist. bat. The content is only in the dir *. */B> batch file.
★Multiple directories appear: If you want to list the content under the root directory of drive C and drive D at the same time, you only need to enter: Dir C: D. [