SoFunction
Updated on 2025-04-08

Batch delete empty folders

Implementation code:

Copy the 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 path so that set route_deepest="%%~dpi" can get 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 MS-DOS window of Windows (Windows 2000/XP is called the command line prompt), enter the drive to find the empty folder, and execute:
dir /ad/b/s | sort /r >。 
Tips
Copy the 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, a file will be generated in the current directory, which contains a list of all folders arranged in reverse order. Open the file with Word or Notepad, put each line into a pair of English quotes, and then insert the "rd " prefix (add a space after rd).

Taking Word as an example, press Ctrl+H to open the "Find and Replace" function, and find "^p" (excluding 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 Character → Paragraph Mark"), replace it with "^prd " (add a space after rd), and then manually correct the contents of the first and last lines. Save the modified file and enter the command line window to execute this batch file. Each folder will be deleted (that is, the function of the rd command), but the folder containing the content cannot be deleted with the rd command, and as a result, all empty folders are deleted.

Knowing why: Let's understand the running principle of the previous Dir command: "/ad" requires the Dir command to select all directories; "/b" means that only the folder name is displayed (no title information or summary of the Dir command); "/s" means search, that is, searching for the current folder and all subfolders. The output of the Dir command is passed to the Sort command via a pipeline command ("|"). The function of the Sort command is to sort, and the "/r" option represents reverse order.

Finally, the output of the Sort command is redirected (">") to the file. Since the Sort command is required to arrange the output results in reverse order, the subfolders in the file are always located 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. You can see that using Windows built-in commands cleverly, many tasks that seem complex on the surface can be easily completed!

Tips: Dir's three tricks 

★View all hidden files in folders: If you want to view all hidden files in Windows folders and all its subfolders, just enter: DIR C:\Windows.* /AH/S/B/P.

★Easy to create a long file name file list: You can create a batch file such as DIR*.* /B >, copy it to the folder where you want to create a file list, and then double-click to run to save the list of all files in the folder to the file.

★Multiple directories appear together: If you want to list the contents in the root directory of disk C and disk D at the same time, then just enter: DIR C: D:. [