Use the FORFILES command to delete the batch processing of SQLServer backup

Source: Internet
Author: User
Using these parameters, we can construct the following example to solve the problem of deleting backup script files. You can create a script based on the modified date or backup type. You can even construct scripts that can reference both standards.

Using these parameters, we can construct the following example to solve the problem of deleting backup script files. You can create a script based on the change time, date, or backup type. You can even construct scripts that can reference both standards.

Although there are many ways to solve this problem, we can use T-SQL code to handle this file deletion process. I use the xp_cmdshell and FORFILES commands to process a process similar to the one you listed. You can obtain reliable FORFILES information from Microsoft TechNet, but in this article, I will involve a large number of FORFILES structures and usage methods to achieve your goal.

The FORFILES command selects a subset of the file and executes a command for this subset. This command requires the following parameters and accepts the following variables.

Parameters

Variable

Using these parameters, we can construct the following example to solve the problem of deleting backup script files. You can create a script based on the change time, date, or backup type. You can even construct scripts that can refer to two standards at the same time.

We will take a closer look at these possible scripts. Remember, you will execute this process from within the T-SQL code, so you need to summarize these statements in the format of EXEC xp_cmdshell 'forfiles command' in an xp_cmdshell access. Note that in all examples, the/Q and/F labels are used as the DELETE command. This means that this command will use quiet mode (/Q) or even delete files that can only be read (/F ).

Example

When the file change date is later than 10/18/2008, delete all C: Backup directories and. SQL files in its subdirectories.

EXEC xp_cmdshell 'forfiles/p c: BACKUP/s/m *. SQL/d 10/18/2008/c "CMD/C del/Q/F @ FILE "'

When the file is changed more than 30 days ago, delete all C: Backup directories and. SQL files in its subdirectories.

EXEC xp_cmdshell 'forfiles/p c: BACKUP/s/m *. SQL/d-30/c "CMD/C del/Q/F @ FILE "'

When the file is changed more than 30 days ago and the file name starts with "F _", delete all C: Backup directories and. SQL files in its subdirectories.

EXEC xp_cmdshell 'forfiles/p c: BACKUP/s/m F _ *. SQL/d-30/c "CMD/C del/Q/F @ FILE "'

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.