Batch Processing of backup files 7 days ago after automatic creation of scheduled tasks

Source: Internet
Author: User
Tags schtasks

Due to the large number of servers in the company, the daily backup log files created previously accumulate over time.

Very large.

And there is no need to keep that much, according to the company's requirements only need to keep a week.

1. Is there any way to automatically delete the *. log File backed up seven days ago?

2. There are too many servers, and it is impossible to manually create one by one. Is there any batch processing that automatically completes this creation plan task?

The first solution is to automatically delete the log files backed up seven days ago, and then create

Run the batch automatically deleted once on the specified date of the week. I: Delete files 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 whose suffix is log seven days ago. If today is 6.29, 6.22 will be deleted

Files created before. Will not be deleted to the recycle bin.

The specified path is the path of the folder to be deleted. This batch can be placed in any folder..

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

Parameter description:

Here/p refers to the path of the backup file to be deleted

/M indicates the character to be matched, The wildcard is *, and the suffix can be modified based on the actual situation..

Such as zzw *. log; *. bak; *. txt
-D refers to the date, and "-d-7" refers to seven days from the current date but before.

/C refers to the execution of commands

@ Path: returns the complete file path.
Method 2:

Description: You can delete a log file whose suffix is log seven days ago.

You do not need to specify a path. The path must be placed in the file to delete the log.. Delete matching files in the current folder.

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

The above batch processing has passed the test on a windows 2003 Virtual Machine. Method 3: It is provided by netizens and can be applied to xp and 2000. forfiles is available only for systems above 2003 by default. : Delete the txt file from the F: \ file backup seven days ago
: The date of the deleted file is based on the file creation date.
@ Echo off
: The date before 7 days.
> Deldata_z1_zhu.vbs echo wscript. echo dateadd ("d",-7, date)
For/f "tokens = 1, 2, 3 * delims =-" % I in ('cscript/nologo deldata_z1_zhu.vbs ') do (
Set y = % I
Set m = % j
Set d = % k
)
If % m % LSS 10 set m = 0% m %
If % d % LSS 10 set d = 0% d %
Set rq = % y %-% m %-% d %
Echo Note: files created earlier than % rq % will be deleted soon
Pause
: File judgment
For/f "skip = 4 delims =" % I in ('dir/tc F: \ TEST \ *. log') do call: w "% I"
Del deldata_z1_zhu.vbs
Goto: eof
: W
For/f "tokens = 4 delims =" % a in (% 1) do set str = %
Set var = % 1
Set num = % var :~ 1, 10%
If % num % lss % rq % del/q F: \ TEST \ % str % II: Automatically create a scheduled task that runs once a week

You can use the at and schtasks commands to create scheduled tasks. schtasks provides many 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 0
Start % systemroot % \ tasks
Echo created successfully. Please check...
Pause
Del/f Create. bat parameter explanation:

Schtasks/create indicates creating a scheduled task.

/Tn specifies the name of the scheduled task

/Ru indicates the account that runs the batch processing. If this parameter is removed, the current account runs by default and a password is prompted.

The account used for a scheduled task will no longer run successfully if the password changes after the batch processing

Therefore, if I use the system Account system, the password will be omitted.
/Rp password of the specified account

/Tr specifies the path of the program, which is the path of the batch processing to be executed.

/SC is the specified running cycle

/D is the date. Here mon is the abbreviation of the English word, Monday

/St indicates the running time.

The start % systemroot % \ tasks statement is only used to display the scheduled task window to check whether the task is successfully created.

Del/f Create. bat: Batch Processing of the scheduled task automatically deleted after the task is created

For more information about the schtasks command, see

Http://technet.microsoft.com/zh-cn/cc772785 (WS.10). aspx # BKMK_create

 

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.