?
Solved

Remove old files in a directory

Posted on 2011-05-09
5
Medium Priority
?
442 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:spiderwilk007
5 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 35720550
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
0
 
LVL 8

Author Comment

by:spiderwilk007
ID: 35720906
No subordinate folders to worry about, it's a single directory, I want the script to kill anything older than 2 weeks.
0
 
LVL 8

Accepted Solution

by:
GundogTrainer earned 2000 total points
ID: 35721096
I use the attached script to delete any webserver log files older than 14 days.

I just have a sceduled task start a cleanup.bat file and log what its deleted.
If you edit lines 3 and 11 for the path and file type (if needed)

REM cleanup.bat
cscript.exe //nologo cleanlog.vbs >>cleanlog.txt

wscript.echo "Script Started: " & date & ", " & Time
Dim fso, f, f1, fc, strComments, strScanDir
strDir = "C:\WINDOWS\system32\LogFiles\W3SVC1"
strDays = 14

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(strDir)
Set fc = f.files

For Each f1 in fc
   if right(f1.name,4)=".log" then 
      If DateDiff("d", f1.DateCreated, Date)> strDays Then
            strComments = f1.name & " " & f1.DateCreated
            fso.DeleteFile(f1)
wscript.echo strComments
      End If
   end if
Next
wscript.echo "Script Finished: " & date & ", " & Time
WScript.Quit

Open in new window

0
 
LVL 15

Expert Comment

by:sr75
ID: 35721122
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

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
Among the most obnoxious of Exchange errors is error 1216 – Attached Database Mismatch error of the Jet Database Engine. When faced with this error, users may have to suffer from mailbox inaccessibility and in worst situations, permanent data loss.
This video teaches viewers how to encrypt an external drive that requires a password to read and edit the drive. All tasks are done in Disk Utility. Plug in the external drive you wish to encrypt: Make sure all previous data on the drive has been …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month12 days, 16 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question