BAT uses Forfiles to automatically bulk delete expired files _dos/bat

Source: Internet
Author: User

FTP Server to save backup files, but also do not need to keep the data every day, so in order to consider saving hard disk space, you must delete some old files, as long as the last week or one months to save the

Windows has a batch deletion program, called FORFILES,XP, and the bottom of the post can be downloaded.

First of all, I use the FTP server to back up data files every day, only want to save the last two weeks of files

Write a bat file with the following contents

Wrote

@echo off
rem write to log
set Filename=deletefile.log
echo-------------------Delete expired Log Files------------------------->>%filename%
echo Start >>%filename%
Echo (%date%%time%) > >%filename%
echo Delete The expired log files in destination directory path 1, please wait ... >>%filename%
forfiles.exe/p "D: \kugou "/M *.tar/d -14/c" cmd/c del @path "
Echo End >>%filename%
Echo (%date%%time%) >>%filename%

Key command on this one: forfiles.exe/p "D:\Kugou"/M *.tar/d -14/c "cmd/c del @path"
The meaning of this command is to search for a file in the D:/kugou directory named tar, to see if its date attribute is 14 days ago (-14 before, +14 after), and then execute the delete command
In the beginning has been entangled in this date format is how to do, is not forfiles by matching file names to delete, the result has been changed the date of the file name display format, and later found that my IQ is so low, people forfiles is the windows of its own, Ni obviously can intelligently match the file date attribute ah.
So, as long as the date attribute of this file is 14 days ago, it will be deleted by the above script.

Then add the task Schedule ...

Here is a forfiles parameter 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, indicating where to start the search. The default folder is the current working directory, which is specified by typing a period (.).

/M Searchmask

Search for files by Searchmask. The default searchmask is *.*.

/s

Instructs Forfiles to search in subdirectories.

/C Command

Runs the specified command on 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 that is greater than or equal to (+) (or less than or equal to (-)) The specified date, where mm/dd/yyyy is the specified date and DD is the current date minus DD days. If + or-is not specified, the + is used. The valid range for DD is 0-32768.

/?

Displays help at the command prompt.

Comments

Forfiles is most commonly used in batch files.
The forfiles/s is similar to dir/s.
• The following table lists the variables that can be used in the/C command string.

Variable description
@file
Filename

@fname
File name with no extension

@ext
File name extension

@path
Full path to File

@relpath
Relative path of File

@isdir
If the file type is a directory, the evaluates to true, otherwise the value is False

@fsize
File size in bytes

@fdate
Last modified date stamp in file

@ftime
Last modified timestamp in file


• Using 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 with *.txt extensions in the tree. Alternatively, you can use the filename "Myinput.txt" as the first argument, in the c:/ Execute each batch file (*.bat) on the drive.

• By using forfiles, you can perform any of the following actions:

• Use/d to select files by absolute or relative date.

• Build an archive tree of files using variables such as @fsize (file size) and @fdate (file date).

• Use @isdir variables to differentiate between files and directories.

• Format output by including special characters on the command line and using the hexadecimal code 0x HH wrapping character.


Forfiles works by executing the "loop subdirectory" tag on a tool designed to work with only a single file.


Example
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 the 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 that have more than 100 days on the drive C:, type:

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

To list all the files that were created prior to January 1, 1993 on drive C: and for files that have a date earlier than January 1, 1993 to display the 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 0x09@ext0x09" 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 the 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 that have more than 100 days on the drive C:, type:

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

To list all the files that were created prior to January 1, 1993 on drive C: and for files that have a date earlier than January 1, 1993 to display the 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 0x09@ext0x09"

ForFiles.exe of Batch command (32/x64) Download: http://www.jb51.net/softs/498793.html

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.