Removing files from a directory which are over 24 hours old?

Commissario
Joined
16 Oct 2002
Posts
344,001
Location
In the radio shack
I have a directory full of images which gets updated every six minutes and I automatically upload that to a Picasa gallery.

I want to automatically delete files from that directory which are over 24 hours old - Picasa will then automatically remove them from the gallery.

Does anyone know of a free tool which will monitor a directory contents and delete files over a certain age?
 
Run a vbs script every 5 minutes?


Found this on expertsexchange

'=======================
' Create a FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")



' Enter your folder to start deleting files from
startFolder = "C:\TEST"




' Enter the names of folders to exclude from checking:
arrFoldersToExclude = Array("TheVolumeSettingsFolder",".TemporaryItems")
strFoldersToExclude = ";" & Join(arrFoldersToExclude, ";") & ";"
arrFilesToExclude = Array("ds_store")
strFilesToExclude = ";" & Join(arrFilesToExclude, ";") & ";"




' This sets the amount of days old for files to be, before
' they are deleted. The number must be negative.
OlderThanDate = DateAdd("d", -31, Date) ' 31 days (adjust as necessary)

' This calls the function that actually deletes the files.
DeleteOldFiles startFolder, OlderThanDate

Function DeleteOldFiles(folderName, BeforeDate)
Dim folder, file, fileCollection, folderCollection, subFolder

' Get the folder to delete files from
Set folder = fso.GetFolder(folderName)

' Check if the current folder name is in the strFoldersToExclude String
If InStr(LCase(strFoldersToExclude), ";" & LCase(folder.Name) & ";") = 0 Then
' Return a collection of all of the files in that folder
Set fileCollection = folder.Files
' Go through each file....
For Each file In fileCollection
' ... to check if the DateLastModified value is before
' the minimum age of files to delete.
If file.DateLastModified < BeforeDate Then
If InStr(LCase(strFilesToExclude), ";" & LCase(file.Name) & ";") = 0 Then
fso.DeleteFile file.Path, true
End If
End If
Next
End If
' Get the next collection of SubFolders to go through
Set folderCollection = folder.SubFolders

' Go through each subFolder
For Each subFolder In folderCollection
DeleteOldFiles subFolder.Path, BeforeDate
If (subFolder.Files.Count = 0) Then
Call subFolder.Delete()
End If
Next
End Function
'=======================
 
Last edited:
create a file with the extension .vbs and put it in there.

Then you can use scheduled tasks to run it every 5 minutes.

Dont forget to change the folder and days

edit: dont forget to test it on a test folder before letting it loose!
 
OK, all done, thanks. I hope that a setting of -1 days will do the job.
 
In the line

Code:
OlderThanDate = DateAdd("d", -31, Date)
Change the "d" to an "n" and alter the -31 to say -5.

"n" tells it to work with minutes, so you don't have to wait a whole day to test the script works. Once you are happy, change the values back to "d" and -1. Make sure you test on files you either have backed up or don't care about. Just in case! :p
 
Or leave it at n and set it to -1440 and that should be the full 24 hours?
 
.. or not. With it set to n and -5 for testing, it doesn't do anything at all.
 
On the same line change Date to Now.

Date only gets the date, not the specific time. So even if you left it at -1 days, the script wouldn't have ran properly [for your needs] anyway. :)

The line should now read:
Code:
OlderThanDate = DateAdd("n", -5, Now)

Changing the -5 to -1440 once you are happy it works. I have tested it myself just now. :)
 
That works, thanks.

One thing I've just noticed, there's a hidden .ini file in the same directory which I don't want to be deleted. Is there a simple tweak to that script to only have it remove .jpg files?
 
Code:
arrFilesToExclude = Array("ds_store")

Change ds_store to whatever the file is called. You can also add others if need be, just separate the names with commas.

For example
Code:
arrFilesToExclude = Array("picasa.ini")
'or
arrFilesToExclude = Array("picasa.ini", "reallyimportant.doc")
 
Back
Top Bottom