Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete empty folders in batches

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to delete empty folders in batch processing. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Implementation code:

The code is as follows:

@ echo off

For / f "tokens=*" an 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

)

:: remove the last\ of the path so that set route_deepest= "% ~ dpi" can get to the upper 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 where you want to find the empty folder, and execute:

Dir / ad/b/s | sort / r > empties.bat.

Tips

The code is as follows:

The input method for "|" on the command line above is to hold down the shift key and press the "\" key.

Step 2: after the command runs, an Empties.bat file is generated under the current directory, which contains a list of all folders in reverse order. Open the file in Word or notepad, put each line in a pair of English quotation marks, and then insert the prefix "rd" before each line (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 by clicking the "Advanced" button in the "find and replace" dialog box, and then clicking the "special character → paragraph mark"), replace it with "^ prd" (with a space after the rd), and then manually correct the contents of the first and last lines. Save the modified Empties.bat file and go to the command line window to execute the batch file. Empties.bat will try to delete (that is, the role of the rd command) each folder, but the folder containing contents cannot be deleted with the rd command, so all empty folders are deleted.

Know why: let's take a look at how the previous Dir command works: "/ ad" asks the Dir command to select all directories; "/ b" displays only the folder name (not the title information or summary of the Dir command); and "/ s" means search, that is, to find the current folder and all its subfolders. The output of the Dir command is passed to the Sort command through the pipe command ("|"). The function of the Sort command is to sort, and the "/ r" option indicates the 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 subfolder of the Empties.bat file is always in front of the parent folder, so when deleting a folder, the empty subfolder is always deleted first, and the empty parent folder is also deleted. As you can see, with the clever use of Windows built-in commands, many seemingly complex tasks can be easily accomplished!

This is the end of the article on "how to delete empty folders in batches". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report