Batch delete empty folder

Time:2021-6-10

Implementation code:

Copy codeThe 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 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 last of the paths to set route_ Deep =%% ~ DPI “can get the path of the upper layer
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 MS-DOS window of windows (Windows)   2000 / XP is called 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 above command line 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 the list of all the folders arranged in reverse order. Open the file with word or notepad, put each line into a pair of quotation marks, and then insert “Rd” in front of each line  ” Prefix (RD followed by a space).  

Take word as an example, press Ctrl + H to open the “find and replace” function to find “^ P” (without quotation marks, the same below. “^ “P” can be entered in the following ways: click the “advanced” button in the “find and replace” dialog box, and then click “special characters → paragraph mark”) to replace it with “^ PRD”  ”( Rd followed by a space), and then manually correct the first and last lines. Save the modified empties.bat file and enter the command line window to execute the batch file. Empties.bat will try to delete (i.e. the function of RD command) every folder, but the folder containing content cannot be deleted with RD command. As a result, all empty folders are deleted.  

Know why: let’s take a look at the operation principle of the dir command: “ad” requires the dir command to select all the directories/ “B” indicates that only the folder name (not the title information or summary of dir command) is displayed/ S “means search, that is, find the current folder and all its subfolders. The output of the dir command is passed to the sort command through the pipeline command (“|). The sort command is used to sort, and the / R option indicates reverse order.  

Finally, the output of the sort command is redirected (“>”) to the empties. Bat file. Because the sort command is required to sort the output results in reverse order, the subfolders of empties.bat file are always in front of the parent folder. Therefore, when deleting a folder, the empty subfolders are always deleted first, and the empty parent folder is also deleted. We can see that many seemingly complex tasks can be easily completed by skillfully using Windows built-in commands!  

Tips: three tips of dir 

View all hidden files in the folder: if you want to view all hidden files in the windows folder and all its subfolders, just enter: dir   C:\Windows.*  / AH/S/B/P。  

Easy to create long file name file list: you can create a batch file, such as getlist.bat, which contains only dir  *.* / B  > List.txt, copy it to the folder where you want to create the file list, and then double-click to run to save the list of all files in the folder to the list.txt file.  

If you want to list the contents in the root directory of Disk C and disk d at the same time, just enter: dir   C:   D: That’s it[