Need help with script on deleting Folders older than X days
Here is what i have so far, it deletes the files in the folder, but i cannot get it to delete the actual folder. Any help would be greatly appreciated:
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
strSourceFolder = "c:\test"
intDel = 0
dtOld = DateAdd("d", -intDel, Date)
Set objFolders = objFSO.GetFolder(strSourceFolder)
For Each fil In objFolders.Files
If DateDiff("d", fil.datelastaccessed, dtOld) >= intDel Then
objFSO.DeleteFile fil.Path
End If
Next
Set objFSO = Nothing
wscript.quit
Answers (4)
http://www.mojobudgie.com/a-simple-batch-script-to-delete-folders-older-than-x/
doesn't seem to work get this error with this syntax:
ForFiles /P C:backups /D -4 “CMD /C if @ISDIR==TRUE echo RD /Q @FILE &RD /Q /S @FILE”
pause
Comments:
-
does not look like there is a " in front of CMD and at the end looks like more of a O circumflex - SMal.tmcc 11 years ago
I've used this in Powershell before.
$Now = Get-Date #Enter number of days to check here. $Days = "2" #Path to File. $TargetFile = "c:\temp" $LastWrite = $Now.AddDays(-$Days) $Files = Get-Childitem $TargetFile -Recurse | Where {$_.LastWriteTime -le "$LastWrite"} foreach ($File in $Files) { if ($File -ne $NULL) { write-host "Deleting File $File" -ForegroundColor "DarkRed" Remove-Item $File.FullName | out-null } else { Write-Host "No more files to delete!" -foregroundcolor "Blue" } }
If you want to delete the folder, why bother faffing with deleting the files first? Just use the DeleteFolder method! :-)
http://msdn.microsoft.com/en-us/library/ca0at0xh%28v=vs.84%29.aspx
Comments:
-
Well I am running an FTP back up of my k1000 box to a network share which is backed up nightly by Avamar. The k1000 generates a folder that the back ups are in with the same name scheme every night with the date. So i want to delete the old one so we only have the most recent one backed up via Avamar.
that may make no sense, it's early here, haven't had coffee. - areiner 11 years ago