Bat uses forfiles to automatically delete expired files in batches

Posted on

FTP server is used to save backup files, but it does not need to keep data every day. So in order to save hard disk space, it is necessary to delete some old files, as long as the last week or month is saved.

Windows has a batch deletion program called forfiles. It seems that there is no one in XP. The bottom attachment of the blog can be downloaded.

First of all, let’s talk about my application. After backing up the data files every day on the FTP server, I just want to save the files for the last two weeks.

Write a bat file, which reads as follows

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 the target 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%

The key command is this: forfiles.exe/p “D: Kugou”/m*.tar/d-14/c “cmd/c del@path”
This command means searching any file with the suffix tar in the D:/Kugou directory to see if its date attribute is 14 days ago (-14 before, +14 after), and then executing the deletion command.
At first, I was wondering how to get this date format. Is forfiles deleted by matching file names? As a result, I have been changing the date display format of file names. Later, I found that occasionally my IQ is so low. People’s forfiles come from Windows. Nima can obviously match the date attribute of file intelligently.
So, as long as the date attribute of this file is 14 days ago, it will be deleted by the script mentioned above.
 
Then add the task plan…
 
Here’s the parameter usage of forfiles
 
Forfiles
Select files to batch from folders or trees.
 
grammar
forfiles [/p Path ] [/m SearchMask ] [/s ] [/c Command ] [/d [{+ | – }] [{MM / DD / YYYY | DD }]]
 
parameter
/p Path
 
Specify Path to indicate where to start the search. The default folder is the current working directory, which is specified by typing a period (.).
 
/m SearchMask
 
Search the file according to SearchMask. The default SearchMask is *. *.
 
/s
 
Indicates forfiles to search in subdirectories.
 
/c Command
 
Run 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 whose date 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, use +. The effective range of DD is 0 – 32768.
 
/?
 
Display help at a command prompt.
 
Notes
 
Forfiles are most commonly used in batch files.
Forfiles/s is similar to dir/s.
The following table lists variables that can be used in the / C Command command string.
 
Variable Description
@file
Filename
 
@fname
Name of file without extension
 
@ext
File extension
 
@path
The full path of the file
 
@relpath
Relative path of file
 
@isdir
If the file type is a directory, the value is TRUE, otherwise FALSE
 
@fsize
File size in bytes
 
@fdate
Date stamp of the last modification in the document
 
@ftime
Timestamp of the last modification in the 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 in the tree with the *. TXT extension. Alternatively, each batch file (*. bat) can be executed on a C:/ drive using the file name “Myinput. txt” as the first parameter.
 
By using forfiles, any of the following operations can be performed:
 
Use/d to select documents according to absolute or relative dates.
 
Build an archive tree of files using variables such as @fsize (file size) and @fdate (file date).
 
Use the @isdir variable to distinguish between files and directories.
 
Format the output by including special characters on the command line and using the hexadecimal code 0x HH surround characters.
 
 
Forfiles works by executing “circular subdirectory” tags on tools designed to process only individual files.
 
 
Example
To list all 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: that have been in existence for more than 100 days, type:
 
forfiles /p c:/ /s /m*.* /dt-100 /c”cmd /c echo @file :date >= 100 days”
 
To list all files created before January 1, 1993 on drive C, and to display “file is quite old!” for files dated earlier than January 1, 1993, type:
 
forfiles /p c:/ /s /m*.* /dt-01011993 /c”cmd /c echo @file is quite old!”
 
To list all file extensions 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 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: that have been in existence for more than 100 days, type:
 
forfiles /p c:/ /s /m *.* /d t-100 /c “cmd /c echo @file :date >= 100 days”
 
To list all files created before January 1, 1993 on drive C, and to display “file is quite old!” for files dated earlier than January 1, 1993, type:
 
forfiles /p c:/ /s /m *.* /d t-01011993 /c “cmd /c echo @file is quite old!”
 
To list all file extensions on drive C: in column format, type:
 
forfiles /p c:/ /s /m*.* /c “cmd /c echo extension of @file is 0x09@ext0x09”

Download the batch command forFiles.exe (32/x64): //www.jb51.net/softs/498793.html

Leave a Reply

Your email address will not be published.