Another one:
Dim fso, startFolder, OlderThanDate
Set fso = CreateObject("Scripting.Fi
startFolder = "C:\some\folder\" ' folder to start deleting (subfolders will also be cleaned)
OlderThanDate = DateAdd("d", -3, Date) ' 3 days (adjust as necessary)
DeleteOldFiles startFolder, OlderThanDate
Function DeleteOldFiles(folderName,
Dim folder, file, fileCollection, folderCollection, subFolder
Set folder = fso.GetFolder(folderName)
Set fileCollection = folder.Files
For Each file In fileCollection
If UCase(Right(file.Name, 4)) = ".BKF" Then
If file.DateLastModified < BeforeDate Then
fso.DeleteFile(file.Path)
End If
End If
Next
Set folderCollection = folder.SubFolders
For Each subFolder In folderCollection
DeleteOldFiles subFolder.Path, BeforeDate
Next
End Function
Main Topics
Browse All Topics





by: schworakPosted on 2006-11-03 at 08:27:17ID: 17867682
Dim fso As Object lesystemob ject")
Dim f As Object
Set fso = CreateObject("scripting.fi
For Each f In fso.GetFolder("C:\").Files
Debug.Print f.Name, f.DateLastModified
Next f
Set f = Nothing
Set fso = Nothing
Now this sample will only list the files and date they were last modified. To actually make the script delete, you will need to change the DEBUG.PRINT to this...
if f.datelastmodified < dateadd("d",-7,now()) then
f.delete
end if
The "d" means we are adding days
The -7 means we are adding minus seven days or subtracting 7 days from today's exact date and time.
Play with it and get it to do what you really want.
PS: Don't forget to run this on a test forlder or bad things will happen to your files.