July 30, 2015 at 1:51 pm
Hello Experts,
If I run the below command, it helps to clean up the files inside the folders but unable to clean up the folder and sub-folders older than 30 days.
If you have anything handy with you, please share with me
xp_cmdshell 'forfiles -p "D:\abcd\" -s -m *.* -d -30 -c "cmd /c del /Q @path"'\
Thanks.
August 3, 2015 at 9:34 am
SQL-DBA-01 (7/30/2015)
Hello Experts,If I run the below command, it helps to clean up the files inside the folders but unable to clean up the folder and sub-folders older than 30 days.
If you have anything handy with you, please share with me
xp_cmdshell 'forfiles -p "D:\abcd\" -s -m *.* -d -30 -c "cmd /c del /Q @path"'
In powershell:
# set folder path
$dump_path = "D:\abcd\"
# set min age of files
$max_days = "-30"
# get the current date
$curr_date = Get-Date
# determine how far back we go based on current date
$del_date = $curr_date.AddDays($max_days)
# delete the files
Get-ChildItem $dump_path -Recurse | Where-Object { $_.LastWriteTime -lt $del_date } | Remove-Item
gsc_dba
August 3, 2015 at 5:24 pm
Thanks GSC.
Even I am using the below one. It worked for few days, but now I find that it hangs in the server. I used this code in job step 2, which ran for 4 days and I killed that today in the morning. Not sure, what happened.
DECLARE @pscmd varchar(1000)
DECLARE @targetpath varchar(8000)
DECLARE @olddays int
DECLARE @extension varchar(5)
DECLARE @cmdstring varchar(1000)
SET @targetpath = 'D:\bkp\data\output_results';
SET @olddays = -30 --pass the days with negative values
--SET @extension = 'txt'
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
Thanks.
August 4, 2015 at 9:30 am
Finally I am able to clean up files and folders using the below Powershell script
cd "d:\output\"
Get-Childitem | Foreach-Object {if ($_.LastAccessTime -le (get-date).adddays(-30)) {remove-item -recurse -force $_}};
Can you suggest how to translate that to a good function?
Thanks.
August 5, 2015 at 7:18 am
You almost have a working function with the 2 scripts you already provided.
Just combine the two!
gsc_dba
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply