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 "'