Use the forfiles command to bulk delete n-day-ago Files _dos/bat

Source: Internet
Author: User

On the one hand, in order to facilitate the rapid restoration of the database, in addition to backup redundancy, Backup mode (first back to local, then tape), the database backup of several SQL SERVER 2000 in the hands, will be previously through Symantec Backup The jobs on the exec direct backup are changed to the following ways:
Step 1: The backup is generated from the database maintenance plan on the local disk m, the full backup is kept for 2 days, and the transaction log backup is retained for 3 days

M:\DB_BACKUP\FULL_BACKUP
M:\DB_BACKUP\LOG_BACKUP

Step 2: After the backup completes, the backup file is closed to tape via Symantec Backup Exec.
However, it was found that even if the database maintenance plan for SQL SERVER 2000 was set to delete backup files from a few days ago, it was found that no expired backups were deleted at all. So I had to use a DOS command to handle it. At first I wanted to use the Forfiles command, and when I searched, I found that there was no forfiles command under Windows 2000, and later, by copying from a third party, I found that forfiles could also be used under Windows 2000 (without the help of the Forfiles command, This is done directly with the batch command, and it's a pain in the dead.
The syntax for the forfiles command is shown below

C:\>forfiles/?
forfiles [/P pathname] [/M searchmask] [/S]
[/C command] [/d [+ |-] {YYYY-MM-DD | dd}]
Description:
Selects a file (or set of files) and executes a
command on that file. This is helpful for batch jobs.
Parameter List:
/P pathname Indicates the path to start searching.
The default folder is the current working
Directory (.).
/M Searchmask Searches files according to a searchmask.
The default searchmask is ' * '.
/S instructs Forfiles to recurse into
Subdirectories. Like "dir/s".
/C command indicates the command to execute for each file.
Command strings should is wrapped in double
Quotes.
The default command is "cmd/c echo @file".
The following variables can be used in the
Command string:
@file-returns The name of the file.
@fname-returns the file name without
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 a file type is
Directory A, and "FALSE" for files.
@fsize-returns the size of the file in
bytes.
@fdate-returns the last modified date of the
File.
@ftime-returns the last modified the
File.
To include special characters in the command
Line, use the hexadecimal code for the character
In the 0xHH format (ex. 0x09 for tab). Internal
CMD.exe commands should is preceded with
"Cmd/c".
/d date selects files with a modified date greater
than or equal to (+), or less than or equal to
(-), the specified date using the
"YYYY-MM-DD" format; or selects files with a
Last modified date greater than or equal to (+)
The current date plus "dd" days, or less than or
Equal to (-) the current date minus "DD" for days. A
Valid "DD" Number of days can is any number in
The range of 0-32768.
"+" is taken as default sign if not specified.
/? Displays this help message.

Examples:

    forfiles/?
    forfiles
    forfiles/p c:\windows/s/M dns*.*
    forfil ES/S/M *.txt/c "cmd/c type @file | More
    forfiles/p c:\/s/m *.bat
    forfiles/d -30/m *.exe
  & nbsp;         /C "cmd/c Echo @path 0x09 is changed days ago"
     forfiles/d 2001-01-01
            /C "Cmd/c Echo @fname is new since 1st 2001"
    forfiles/d +2014-12-15/c "cmd/c echo @fname is NE W 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
 
corresponding Chinese message As follows:

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.
As shown below, because the version of the Forfiles command that is copied from Windows Server 2000 is v 1.1, the parameter must be-p,-C,-M, and there should be no space after the argument.

As shown below, Delete_old_backup.bat deletes a full backup of 2 days ago, a transaction log backup, and a maintenance plan-generated log file.

echo--------------------------------------------->>delete_old_backup.log echo Delete the backup log start at% 
 
Date%-%time% >>delete_old_backup.log rem Delete days. 
 
Set daysago=2 REM Delete old backup log files. Set Logpath=m:\db_backup\ forfiles-p%logpath%-m*.txt-d-%daysago%-C "cmd/c del/q @FILE" >> delete_old_backup. Log echo Delete the backup log Stop at%date%-%time% >>delete_old_backup.log Echo Deletes the full backup STA RT at%date%-%time% >>delete_old_backup.log set Fullbackuppath=m:\db_backup\full_backup forfiles-p%fullback  uppath%-m*.bak-d-%daysago%-C "cmd/c del/q @FILE" >> delete_old_backup.log echo Delete the full backup Stop at %date%-%time% >>delete_old_backup.log echo Delete the log backup start at%date%-%time% >>delete_old_b Ackup.log set Logbackuppath=m:\db_backup\log_backup forfiles-p%logbackuppath%-m*. trn-d-%daysago%-C "cmd/c del/q @FILE" >> delete_oLd_backup.log echo Delete The log backup Stop at%date%-%time% >>delete_old_backup.log Echo---------------- ----------------------------->>delete_old_backup.log

After scripting, successful testing, and then setting up task Schedule, the following steps are generally shown:

Step 1: Locate the task schedule in the Control Panel and execute the Task Scheduler Wizard:

Step 2: Click the "Browse" button, select M:\DB_BACKUP\delete_old_backup.bat file

Step 3: Enter the name of the task and the schedule to perform the task

Step 4: Set the time and frequency of task execution

Step 5: Enter the account and password for knowing the scheduled task

Step 6: Complete the task scheduling settings.

The above bat is mainly to support the reality of parameters and logs, really good, in fact, the core code is like this

forfiles/p "C:\Backup"/d-10/c "cmd/c echo deleting @file ... && del/f @path"

It's best to back up your data before testing

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.