The method of periodically deleting all subfolders and files within a folder _dos/bat

Source: Internet
Author: User
Tags first row

In Win9x's time, this was a problem to be solved with the deltree external command, but since Windows 2000 and Windows XP canceled this command (which is really weird), how do you solve it now? You may think of using DEL+RD to solve this problem. One problem, however, is that the RD command deletes the directory you specified. For example, the current directory structure is as follows:
D:\>ATTRIB/S/d D:\test\*.*
A R I D:\test\1.txt This file with read-only properties
A H I D:\test\2.txt This file with hidden attributes
A S I D:\test\3.txt This file with System Properties
H I D:\test1\1 This directory with hidden attributes
A R I D:\test\1\4.txt This file with read-only properties
S I D:\test1\2 This directory with system attributes
A R I D:\test1\2\5.txt This file with read-only properties

Batch Principle If you do this command rd d:\test, then Rd will delete the test directory along with the contents of it, and we'll keep the test directory. So what do we do? We can delete the files in the D:\test directory first by using Del, and then delete them with the RD handle directory. The contents of this batch are as follows: You will save the following as a text file with a suffix of. bat or. cmd, for example, call Deltree.bat.
---does not include this row--
Attrib-s-h-r%1\*.* && del%1\*.*/q
Dir%1/ad/b/S >del.txt
for/f%%i in (del.txt) do rd%%I/S/q
---don't include this line--
So when we need to delete the test directory, we do the "Deltree.bat d:\test" (without quotes).

Batch processing parameters are read by the first line of   in the first row batch of batches, which becomes so attrib-s-h-r d:\test\*.* && del d:\test\*.*/q
First use attrib to remove all files under the test directory, to ensure that the following del smooth execution.
&& is a join symbol that executes the command following the connector after the preceding command is executed. The
del deletes all files under the test directory because the RD command cannot delete the files in the test directory. OK, after the files are removed from the test, we begin to delete the subdirectories under the test directory.

The second row of the batch processing   batch execution to the second row,%1 reads the batch parameter, which is the d:\test we entered after the deltree, and becomes so dir d:\test /ad/b/s > Del.txt here the parameters of the dir command have the following meanings:
 /AD          Displays the directory with the specified attributes. This allows the following Rd command to delete these directories
/b          use empty format (no header information or summary). Avoid the subsequent deletion command error because the dir command also prints some additional information, such as "the volume in drive C does not have a label." The serial number of the   volume is Ee5b-d3af ", which obviously causes an RD command error. The
/s          displays the files in the specified directory and all subdirectories. The full path of the output directory and file to facilitate the subsequent RD command deletion.
After this row of batches, the Del.txt file is generated in the current directory and you perform type del.txt and you see the following output:
d:\test1\1
d:\test1\2
Why do you want to output to a text file? Why not just export the results of dir to Rd? Since Dir will have more than one line of results, wouldn't it be too much trouble to write a line? And in the future, when we actually use batch processing, we can't predict how many subdirectories there will be in the target folder. Isn't it convenient to output the dir result to a text file and then let Rd read it?

Batch third line   Well, we've already exported the list of subdirectories to the text file Del.txt, so just delete them using the RD command. But in fact Rd itself does not have the ability to read text files, we use for to solve this problem. Batch execution to the last line: for/f%%i in (del.txt) do rd%%i/s/q 
%%i is a loop variable used in a For statement that reads one row from the del.txt for each use, as a variable value, and the for This variable is passed to the command behind do execution, so the order of the For loop execution is as follows:
First Cycle rd D:\TEST1\1/S/q
Second Cycle rd D:\TEST1\2/S/q
gnaw0725 Note: about for For more explanations of loops please refer to  http://www.jb51.net/article/31284.htm
The RD parameter here has the following meanings:
/s      All subdirectories and files under the specified directory are also deleted, in addition to the directory itself. Used to delete a directory tree.
/q      Quiet mode, do not require confirmation when deleting a tree with/S

troubleshooting Well, to this point, deltree.bat This batch removes all files and folders under D:\test, regardless of the attributes they take. If you have problems with this batch process, such as not deleting files, how do you troubleshoot? We can have two check point:
Batch processing parameter acquisition is to investigate whether the batch processing correctly obtained the parameters, that is, we entered the target folder, we can add an echo%1 in the front of the batch, see the result, is this:
Echo%1
Attrib-s-h-r%1\*.* && del%1\*.*/q
gnaw0725 Note: Batch processing can have 9 parameters,% 0 represents batch processing itself, the first parameter% 1, the second%2, ... So
The subdirectory output list is del.txt, you can enter type Del.txt, and if normal, the output should be all subdirectories under D:\test.

Small series of cloud-dwelling communities supplement:

In fact, the delete file and folder commands with Rd simpler and more rough, specific can refer to the following command:

Copy Code code as follows:

RD/S/q F:\DDD

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.