SQL server agent job that housekeeps obsolete physical files

SET @CommandDelete1 = 'DEL' +  ' ' + RTRIM(@SourcePath1)
						exec @ZeroOneX = xp_cmdshell @CommandDelete1
						Set @ChkName = RTRIM(REPLACE(substring(@xFileName,1, len(@xFileName) - 16),'file-',''))
						SELECT @FileLocation1 = RTRIM(@DestinationPath) + RTRIM(REPLACE(@xFileName,'.int',''))

my file name is something link this: file-0001-tgitm-20150515-1050-001.int
20150515 - this is dated (May 15 2015). How can I delete only the files older than 10 days?

  1. you have get the forfiles working on UNC path ?

  2. Isn't the file date time reflect the file creation date ? If it is not, then forfiles will not be able to do that. The /D parameter is based on file date time. You will need to write script to do that. Windows Powershell script should be able to do the job.

i have come up with that fragment code to delete the files from a certain location... i will be deleting based on the filen ame now.

my file name is something link this: file-0001-tgitm-20150515-1050-001.int
20150515 - this is dated (May 15 2015). How can I delete only the files older than 10 days? Please advice, thanks! :smile:

Write a Powershell Script / Windows Script ( VB, JScript ) to do that.

DECLARE

@DestinationPath char(200),
@xFileName varchar(100),
@DesPathX1 char(200),
@Delete1 char(200),
@ZeroOnex2 int

select @DestinationPath = '\\10.7.11.156\sbom_files\SEHK\FOR_ISP'
select @xFileName = 'sehk-0001-tgitm-20150508-1901-001.int'

Set @DesPathX1 = RTRIM(@DestinationPath) + RTRIM(REPLACE(@xFileName,'.int',''))
SET @Delete1 = 'DEL' + ' ' + RTRIM(@DesPathX1)
exec @ZeroOnex2 = xp_cmdshell @Delete1

i was able to delete the filename hardcoded above, any idea how to delete based on the filename older than 10 days?