Batch implementation of periodic actions to automatically delete expired files

Source: Internet
Author: User

Log files are generated in the same directory after execution: the starting point of the DelExpiredLog.log reality execution.
@echo off
REM Write to log
Set Filename=delexpiredlog.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/p "Destination directory path 1"/m/*/d-3/C "cmd/c del @path"
echo Delete The expired log files in destination directory path 2, please wait ... >>%filename%
forfiles/p "Destination directory path 2"/m/*/d-3/C "cmd/c del @path"
Echo End >>%filename%
Echo (%date%%time%) >>%filename%
Exit

forfiles [/P pathname] [/M searchmask] [/S] [/C command] [/d [+ |-] {YYYY-MM-DD | dd}]

Describe:
Select a file (or a set of files) and on that file
Executes a command. This helps with batch jobs.

Parameter list:
/P pathname Indicates the path to start the search. The default folder is the current work
Directory (.).

/M Searchmask search for files based on the search mask. The default search mask is ' * '.

/S Guide Forfiles recursion to subdirectories. Like "dir/s".

/C command represents the commands executed for each file. The command string should
Enclose in double quotation marks.

The default command is "cmd/c echo @file". The following variables
Can be used in the command string:
@file-Returns the file name.
@fname-Returns the file name without an extension.
@ext-returns only the extension of the file.
@path-Returns the full path of the file.
@relpath-Returns the relative path of the file.
@isdir-Returns "TRUE" if the file type is a directory;
If it is a file, return "FALSE".
@fsize-Returns the file size in bytes.
@fdate-Returns the date the file was last modified.
@ftime-Returns the time the file was last modified.

To include special characters at the command line, use the character 0xHH
The form uses hexadecimal code (for example, 0x09 is tab).
The internal CMD.exe command should begin with "CMD/C".

/d Date Select the file whose last modification date is greater than or equal to (+)
or less than or equal to (-) the date specified in the "YYYY-MM-DD" format;
Or select a file whose last modification date is greater than or equal to (+)
Current date plus "DD" day, or less than or equal to (-) current
Date minus "DD" days. A valid "DD" number of days can be
Any number in the range 0-32768. If not specified,
"+" is used as the default symbol.

/? Displays the help message.

For example:
Forfiles/?
Forfiles
forfiles/p c:\windows/s/M dns*.*
FORFILES/S/M *.txt/c "cmd/c type @file | More
forfiles/p c \/s/m *.bat
FORFILES/D -30/m *.exe
/C "cmd/c Echo @path 0x09 was changed before 30. "
FORFILES/D 2001-01-01
/C "cmd/c Echo @fname is new on January 1, 2001. "
FORFILES/D +2006-11-25/c "cmd/c Echo @fname is new today. "
forfiles/m *.exe/d +1
FORFILES/S/M *.doc/c "cmd/c echo @fsize"
forfiles/m *.txt/c "cmd/c if @isdir ==false notepad.exe @file"

Delete files from 30 days ago forfiles/p "d:\108"/s/m * * */d-30/c "cmd/c del @path"

Batch implementation of periodic actions to automatically delete expired files

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.