Script to delete files older than 30 days?

Soldato
Joined
7 May 2004
Posts
5,503
Location
Naked and afraid
Hi guys,

I've searched the web for a script that will do this, I've returned quite a lot of results including the following:

Option Explicit

' Set constants for log file paths and maximum age of files
const LogPath1 = "e:\Cognos\Temp\test"
Const LogFileAge = 30

'WScript.Echo LogPath1
DeleteFilesByDate logpath1, LogFileAge



Sub DeleteFilesByDate(ByVal path, ByVal age)
' Deletes files over a certain age
Dim objFSO
Dim objFolder
Dim colFiles
Dim objFile

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(path)
Set colFiles = objFolder.Files
'WScript.Echo "More than " & age & " days old:"
For Each objFile In colFiles
' If file is over age then delete
If (objFile.DateCreated < Date - age) Then
'WScript.Echo "Deleting..."
'WScript.Echo objFile.Name & ": " & objFile.DateCreated
objFile.Delete
End If
Next
' Tidy up
Set objFSO = Nothing
Set objFolder = Nothing
Set colFiles = Nothing
End Sub

This script runs with no errors but doesn't touch any files - any ideas why?
 
Is there any reason why this wouldn't work on a Win2k3 server? It literally runs and does bugger all for me!
 
Not that I know of. Try testing it on some other files also chuck in a couple of WScript.Echo statements so you can see what your checking.

ClearDate = Date - age

WScript.Echo ClearDate

I'm guessing something isn't evaulating as you intended eg it's checking for todays date or even a date in the future.
 
I copied over a number of files and they didn't delete because I guess they had todays date?

I tested it on an untouched directory and it worked fine!
 
Back
Top Bottom