The VBScript Network and Systems Administrator's Cafe

May 2 2009   6:19PM GMT

Using the VBScript datediff function to determine the age of a file by the last modified and the last accessed time

Jerry Lees Jerry Lees Profile: 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.


Option Explicit

Dim fName
Const DateLastModified = 1, DateLastAccessed = 2


if FileAge(fname,5,DateLastAccessed) = True Then
WScript.Echo (“The file was accessed recently enough!”)
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!”)
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

Select Case CompareType
Case 1
DateDifference = DateDiff(“n”,LastModified, Now())
Case 2
DateDifference = DateDiff(“n”,LastAccessed, Now())

End Select
If DateDifference > fage Then
FileAge = False
FileAge = True
End If
End Function

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: