Automatically delete files from N days ago under Windows

Source: Internet
Author: User

Using the built-in commands under Windows2003 forfiles with scheduled tasks, you can automate the deletion of files from N days ago.

It is easy to set the scheduled tasks for automatic execution in windows2003.

First, script writing

Forfiles Command usage:

Forfiles

Select the file you want to batch from the folder or tree.

Grammar

forfiles [/P Path] [/m searchmask] [/s] [/C Command] [/D[{+ | -}] [{MM/DD/YYYY | DD}]]

Parameters
/ P Path
Specify Path, which indicates where you want to start the search. The default folder is the current working directory, which is specified by typing a period (.).
Note: Absolute paths should be enclosed in double quotation marks, and the path should be split by backslashes: Example: "D:\HOME\Desktop\62backup"
/ M Searchmask
Search for files by Searchmask . The default searchmask is *. *.
/s
instructs forfiles to search in subdirectories.
/ C Command
runs the specified Commandon each file. command strings with spaces must be enclosed in quotation marks. The default Command is "cmd/c echo @file".
/ D  [{ + | -}]  [{ MM / DD / YYYY | DD}]
Select a file with a date greater than or equal to ( +) (or less than or equal to ( -)) The specified date, where MM / DD / YYYY is the specified date, dd is the current date minus DD days. If + or -is not specified, +is used. The effective range of DD is 0-32768.
/?
displays help at the command prompt.
Comments
  • forfiles are most commonly used in batch files.
  • forfiles/s is similar to dir/s .
  • The following table lists the variables that can be used in the/ C Command command string.
    variables Description
    @file Filename
    @fname File name with no extension
    @ext File name extension
    @path The full path of the file
    @relpath Relative path of the file
    @isdir TRUE If the file type is a directory, otherwise the value is FALSE
    @fsize File size in bytes
    @fdate The last modified date stamp in the file
    @ftime The timestamp of the last modification in the file
  • With forfiles, you can run commands on multiple files or pass parameters to multiple files. For example, you can run the TYPE command on all files in the tree with the *.txt extension. Alternatively, you can use the filename "Myinput.txt" as the first parameter, in the c:/ Each batch file (*.bat) is executed on the drive.
  • By using forfiles, you can perform any of the following actions:
    • Use/ d to select files by absolute or relative dates.
    • Use variables such as @fsize (file size) and @fdate (file date) to build the archive tree for the file.
    • Use @isdir variables to differentiate between files and directories.
    • Format the output by including special characters on the command line and using the hexadecimal code 0xHH surround characters.
  • forfiles works by executing a "loop subdirectory" tag on a tool that is designed to handle only a single file.
Example

Automatically delete. bak files under D:/test for more than 7 days

forfiles/p "D:/test"/s/m *.bak/d -7/c "cmd/c del @path"

To list all the batch files on drive C:, type:

forfiles/p c://s/m*.bat/c "cmd/c Echo @file is a batch file"

To list all directories on drive C:, type:

forfiles/p c://s/m*.*/C "cmd/c if @isdir ==true echo @file is a directory"

To list all files on drive C: With more than 100 days of existence, type:

forfiles/p c://s/m*.*/dt-100/c "cmd/c echo @file: Date >=

To list drive C: Files that were created before January 1, 1993, and for files that have dates earlier than January 1, 1993 to display "file is quite old!", type:

forfiles/p c://s/m*.*/dt-01011993/c "cmd/c Echo @file is quite old!"

To list the extensions for all files on drive C: In column format, type:

forfiles/p c://s/m*.*/C "cmd/c echo extension of @file is [email protected]" With:

To list all the batch files on drive C:, type:

forfiles/p c://s/m *.bat/c "cmd/c Echo @file is a batch file"

To list all directories on drive C:, type:

forfiles/p c://s/m */C "cmd/c if @isdir ==true echo @file is a directory"

To list all files on drive C: With more than 100 days of existence, type:

forfiles/p c://s/m */d t-100/c "cmd/c echo @file: Date >= days"

To list drive C: Files that were created before January 1, 1993, and for files that have dates earlier than January 1, 1993 to display "file is quite old!", type:

forfiles/p c://s/m */d t-01011993/c "cmd/c Echo @file is quite old!"

To list the extensions for all files on drive C: In column format, type:

forfiles/p c://s/m*.*/C "cmd/c echo extension of @file is [email protected]"

Formatting legends

format meaning
Italic body Information that the user must provide
Bold body The element that the user must type exactly to display
Ellipsis (...) Parameters that can be repeated multiple times on the command line
Brackets ([]) Options available
Curly braces ({}); options are separated by a vertical bar (|). For example: {even|odd} The user must select one from the Options collection
Courier 字体 Code or program output

Second, configure the Scheduled tasks

  

    Start-up-All Programs--accessories--System Tools--Task Scheduler       Task Scheduler (local)--task Access library-- > Create a basic task-->      input name:   Description: Click Next       Trigger: Set cycle        set verbose time       action selection launcher       Select the bat script to execute is the script written above       Click Finish       Detailed Properties,  You can tick the pop-up Properties dialog box when you click Done to make detailed settings for each item. Refer to:http://www.jb51.net/os/windows/58393.html  III to verify that the scheduled task can be successfully executed in any execution interface that is scheduled to be configured in advance of the current time 2 minutes ahead of time, Confirm that the scheduled task can execute successfully after the last successful execution (the 0x0 return code indicates a successful execution).  Note: (i) A Chinese garbled behavior may occur when creating a bat file that is called by a scheduled task because it is not ANSI-encoded when the bat file is created, and only ANSI-encoded to support Chinese. Methods to create a bat file that supports Chinese: 1, in the face of this situation is the encoding of different problems, so at the beginning of the code should be corrected, support for Chinese encoding is ANSI.  Our first step is to create a new TXT file. 2, then click on the new text document, and then select "File" = "Save As" 3, CMD encoding is ANSI, if the Chinese is not this encoding method will appear garbled. So we choose "ANSI" when coding 4, and then re-enter the above code into our saved files, the name of the name of the. bat file (ii) created by the bat script double-click on the screen only a flash, you can not view debugging error message resolution 1, execute CMD command 2, Go to the directory where the bat file is located 3, execute the bat file. BAT file executes successfully on the DOS interface, so you can debug the bat script and view the error messages in the script to modify the script statement.   

Automatically delete files from N days ago under Windows

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.