Link to home
Start Free TrialLog in
Avatar of spiderwilk007
spiderwilk007Flag for United States of America

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.
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

spiderwilk007,

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
Avatar of spiderwilk007

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
Avatar of GundogTrainer
GundogTrainer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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()

Open in new window