1. Change the TargetPath
2. Copy the script in SSMS
3. Run it
1. Change the TargetPath
2. Copy the script in SSMS
3. Run it
--Script to delete the old files & Folders Older than 30 days
DECLARE @pscmd varchar(1000)
DECLARE @targetpath varchar(8000)
DECLARE @olddays int
DECLARE @extension varchar(5)
DECLARE @cmdstring varchar(1000)
SET @targetpath = 'D:\SQLBackup\';
SET @olddays = -30 --pass the days with negative values
--SET @extension = 'txt'; --/csv
SET @pscmd = '"& '+
'Get-ChildItem ' + Quotename(@targetpath,'''') + '-Recurse' + ' | '+
'where {$_.lastWriteTime -lt ((Get-Date).AddDays('+ CAST(@olddays as varchar) +')) '+
--'-and ($_.Extension -match ' + QUOTENAME(@extension,'''')+ ') } | ' +
--'-and ($_.Extension -match ' + ') } | ' +
'} |' + 'Remove-Item -force " '
SET @cmdstring = ' ""powershell.exe" '+ @pscmd
select @cmdstring
exec master..xp_cmdshell @cmdstring, no_output