spiderwilk007
asked on
Remove old files in a directory
Hi,
I have a server that I do backups on, I am wondering if there is an easy script or program that can be used for clearing out the oldest files in the directory by deletion.
I have a server that I do backups on, I am wondering if there is an easy script or program that can be used for clearing out the oldest files in the directory by deletion.
Lots of examples on Experts Exchange of this if you want to go with a script approach.
https://www.experts-exchange.com/questions/27026325/Remove-old-files-in-a-directory.html?cid=1576
https://www.experts-exchange.com/questions/24984629/How-do-I-delete-files-by-date-in-a-batch-file.html
https://www.experts-exchange.com/questions/22038195/VBS-Script-Delete-files-check-date-of-files.html
https://www.experts-exchange.com/questions/26912149/deleting-files-over-a-week-old-with-batch-files.html
https://www.experts-exchange.com/questions/27008284/Batch-Script-to-Delete-files-from-Folders-and-Sub-Folders-Older-than-2-months-old.html
My favorite way to do this with a free utility is via DELAGE32.
http://home.mnet-online.de/horst.muc/win/delage.htm
http://home.mnet-online.de/horst.muc/wbat32.htm
~bp
https://www.experts-exchange.com/questions/27026325/Remove-old-files-in-a-directory.html?cid=1576
https://www.experts-exchange.com/questions/24984629/How-do-I-delete-files-by-date-in-a-batch-file.html
https://www.experts-exchange.com/questions/22038195/VBS-Script-Delete-files-check-date-of-files.html
https://www.experts-exchange.com/questions/26912149/deleting-files-over-a-week-old-with-batch-files.html
https://www.experts-exchange.com/questions/27008284/Batch-Script-to-Delete-files-from-Folders-and-Sub-Folders-Older-than-2-months-old.html
My favorite way to do this with a free utility is via DELAGE32.
http://home.mnet-online.de/horst.muc/win/delage.htm
http://home.mnet-online.de/horst.muc/wbat32.htm
~bp
ASKER
No subordinate folders to worry about, it's a single directory, I want the script to kill anything older than 2 weeks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is a script I use that also creates a timestamped circular log file of which files were deleted. The only thing you need to change is the strPath variable.
DIM strPath
DIM FSO
DIM Folder
DIM Files
DIM File
DIM List
DIM intDiff
strPath = "C:\"
set FSO = CreateObject("Scripting.FileSystemObject")
set Folder = FSO.GetFolder(strPath)
set Files = Folder.Files
List = func_TimeStamp() & vbtab & "-----------------------------------" & vbcrlf
List = List & func_TimeStamp() & vbtab & "Beginning Progress Cleanup" & vbcrlf
List = List & func_TimeStamp() & vbtab & FormatDateTime(Now(), 1) & vbcrlf
List = List & func_TimeStamp() & vbtab & "-----------------------------------" & vbcrlf
For each File in Files
intDiff = DateDiff("D", File.DateLastModified, Now())
If intDiff > 30 then
List = List & func_TimeStamp() & vbtab & File.Path & vbcrlf
FSO.deletefile(file)
End if
Next
List = List & func_TimeStamp() & vbtab & "-----------------------------------" & vbcrlf
List = List & func_TimeStamp() & vbtab & "Ending Progress Cleanup" & vbcrlf
List = List & func_TimeStamp() & vbtab & FormatDateTime(Now(), 1) & vbcrlf
List = List & func_TimeStamp() & vbtab & "-----------------------------------" & vbcrlf
func_Log(List)
set strPath = Nothing
set FSO = Nothing
set Folder = Nothing
set Files = Nothing
set File = Nothing
set List = Nothing
set intDiff = Nothing
Function func_TimeStamp()
DIM strYear
DIM strMonth
DIM strDay
DIM strTime
DIM strStamp
strYear = Year(Now)
strMonth = Month(Now)
strDay = Day(Now)
strTime = Time()
If len(strMonth) < 2 then
strMonth = "0" & strMonth
End If
If len(strDay) < 2 then
strDay = "0" & strDay
End If
strStamp = strYear & "-" & strMonth & "-" & strDay & " " & strTime
if len(strStamp) <> 22 then
Do Until len(strStamp) > 21
strStamp = strStamp & " "
Loop
end If
func_TimeStamp = strStamp
set strYear = Nothing
set strMonth = Nothing
set strDay = Nothing
set strTime = Nothing
set strStamp = Nothing
End Function 'func_TimeStamp()
Function func_Log(strLMSG)
DIM strFol
DIM strFile
DIM wFile
DIM rFile
DIM rLine
DIM rLog
DIM rDate
DIM nTime
strFold = "C:\ScriptLogs\"
strFile = strFold & "TRM001_Cleanup.txt"
Timestamp = func_TimeStamp()
If FSO.FolderExists(strFold) = false then
FSO.CreateFolder(strFold)
End If
If FSO.FileExists(strFile) then
Set rFile = FSO.OpenTextFile(strFile)
Do Until rFile.AtEndOfStream
rLine = rFile.ReadLine
If rLine <> "" then
rDate = CDate(split(rLine, vbtab)(0))
nTime = Now()
If DateDiff("D", rDate, nTime) < 90 then
rLog = rLog & rLine & vbcrlf
End If
End If
Loop
rFile.close
End If
set wFile = FSO.OpenTextFile(strFile, 2, True)
wFile.write strLMSG & vbcrlf
wFile.Write rLog
wFile.Close
set strFold = Nothing
set strFile = Nothing
set wFile = Nothing
set rFile = Nothing
set rLine = Nothing
set rLog = Nothing
set rDate = Nothing
set nTime = Nothing
End Function 'func_Log()
1) Does the script have to remove files from subordinate directories as well?
2) Is there a rule to follow, such as "kill everything with DateLastModified 30 or more days ago"?
Patrick