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.

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

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
Else
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.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

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: