Using the VBScript datediff function to determine the age of a file by the last modified and the last accessed time
Posted by: Jerry Lees
I recently needed a way to tell if a file had been accessed or modified recently, something that has always been a question when you’re out of space on a server and can’t just add more space to it. What do you delete??? The Old files are the obvious answer… except if people are using them.
Here is teh script I wrote to tell if a file had been accessed or modified in the last X days. In the example I use 5 days, but you can use a different number of days when you call the function.
Enjoy!
Option Explicit
Dim fName
Const DateLastModified = 1, DateLastAccessed = 2
fName=”c:\temp2.txt”
if FileAge(fname,5,DateLastAccessed) = True Then
WScript.Echo (”The file was accessed recently enough!”)
Else
WScript.Echo (”The file was not accessed recently enough!”)
End If
if FileAge(fname,5,DateLastModified) = True Then
WScript.Echo (”The file was modified recently!”)
Else
WScript.Echo (”The file was not modified recently!”)
End If
Function FileAge(fName,fAge, CompareType)
‘function returns True if the file is older than the fAge (File Age) specified and false if it isn’t
Dim LastModified, LastAccessed, FSO, DateDifference
Set FSO = CreateObject(”Scripting.FileSystemObject”)
LastModified = FSO.GetFile(fname).DateLastModified
LastAccessed = FSO.GetFile(fname).DateLastAccessed
Case 1
DateDifference = DateDiff(”n”,LastModified, Now())
Case 2
DateDifference = DateDiff(”n”,LastAccessed, Now())
If DateDifference > fage Then
FileAge = False
Else
FileAge = True
End If
End Function


