Automatically create a scheduled task delete 7 days ago backup file Batch

Source: Internet
Author: User
Tags schtasks

This article is generated in the writing of personal site backup script when you see, stay for reference

Problem scenario: Because my company has too many servers, the daily backup log files that I've created accumulate over the long haul.

And do not need to keep so much, according to company requirements only need to retain one weeks.

1. What is the way to automatically delete the *.log files that were backed up 7 days ago?

2. Too many servers, it is not possible to create one by one manually, there is no automatic completion of this task of creating the batch process?

The first thing to do is to automatically delete the log files that were backed up 7 days ago, and then create one that can automatically create a

Week specifies the time at which the automatically deleted batch is run once

One: Delete a file that was backed up 7 days ago

Command

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

Method 1:

Description: You can delete a log file with a suffix of 7 days ago named log. If today is 6.29, then 6.22 will be deleted.

file that was previously created. will not be deleted to the Recycle Bin.

specifies that the path is the folder path to be deleted, which can be placed under any folder .

forfiles/p d:\aizzw\log/m *.log-d -7/c "cmd/c del/f @path"

Parameter explanation:

Here,/P refers to the path of the backup file to be deleted

/M refers to the character to match, the wildcard character is the * number, and the suffix name can be modified according to the actual situation .

such as Zzw*.log;*.bak;*.txt

-d refers to the date, "-d-7" refers to the current date from the previous date minus 7 days forward.

/C refers to executing commands

@path return file Full path

Method 2:

Description: You can delete a log file with a suffix of 7 days ago named log.

You do not need to specify a path and must be placed under the file where you want to delete the log . Deletes the matching file under the current folder.

forfiles/m *.log-d -7/c "cmd/c del/f @path"

The above batch has been tested on a Windows 2003 virtual machine

Method 3:

For users to provide, can be applied to xp,2000, and forfiles by default only more than 2003 of the system.

:: Delete the txt file from the F:\ file backup 7 days ago
:: Deleted file date is based on file creation date
@echo off
:: Date 7 days prior to collection.
>deldata_zw.zhu.vbs Echo WScript.Echo DateAdd ("D", -7,date)
for/f "tokens=1,2,3* delims=-"%%i in (' Cscript/nologo deldata_zw.zhu.vbs ') do (
Set y=%%i
Set M=%%j
Set D=%%k
)
If%m% LSS set m=0%m%
If%d% LSS set d=0%d%
Set rq=%y%-%m%-%d%
Echo Note: Files previously created by%rq% will be deleted
Pause
:: Judge File
for/f "Skip=4 delims="%%i in (' dir/tc f:\test\*.log ') do call:w "%%i"
Del Deldata_zw.zhu.vbs
Goto:eof
: W
for/f "Tokens=4 delims="%%a in (% 1) do set str=%%a
Set var=%1
Set num=%var:~1,10%
If%num% LSS%rq% del/q F:\TEST\%str%

Scheduled Tasks

Create a scheduled task available At,schtasks command, SCHTASKS provides a number of parameters

Command schtasks

schtasks/create [/S system [/u username [/p [password]]]
[/ru username [/rp Password]]/sc schedule [/mo modifier] [/D Day]
[/M months] [/I idletime]/tn taskname/tr taskrun [/st StartTime]
[/ri interval] [{/et endtime |/du duration} [/k]]
[/SD StartDate] [/ed EndDate] [It] [/z] [/F]

Method:

@echo off
Schtasks/create/tn "Del-7_log"/ru system/tr d:\aizzw\del-7.bat/sc

WEEKLY/D mon/st 01:00
Start%systemroot%\tasks
Echo created successfully, please check ...
Pause
del/f Create.bat

Parameter explanation:

Schtasks/create refers to creating a scheduled task

/tn Specify the name of the scheduled task

/RU Specifies the account to run the batch, and if you remove the parameter, you will be prompted to enter the password if the current account is run.

Account used for a scheduled task if the password changes, the batch will no longer run successfully

So I use the System account, the password is omitted to write.

/RP the password for the specified account

/TR specifies the path where the program is located, where the batch storage path is specified for execution.

/SC for the specified run period

/d is a date. Here Mon is the English word abbreviation, Monday

/ST for Run time

Start%systemroot%\tasks This sentence just to pop up the Schedule Task window to see if the creation was successful or not

del/f Create.bat automatically deletes the batch that created the scheduled task after the creation is complete

A detailed explanation of the schtasks command can be found in Microsoft's

Reference Source: http://aizzw.blog.51cto.com/440409/171183

Automatically create a scheduled task delete 7 days ago backup file Batch

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.