Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

minutes since file last access 1

Status
Not open for further replies.

lowbot

MIS
Jan 17, 2008
28
US
Can someone give me a few pointers on how to write a script that will return a value in an integer of minutes since a file has been last accessed?

I found that objFile.LastAccessed will give me this info, but Im not sure how I can turn this value into minutes so I can compare it to the system time. Or if Im going about this the wrong way. Thanks
 
Have a look at the DateDiff function.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I think you are going to find that the DateLastAccessed is highly unreliable.

Give this a try. Just modify the part in red with the path to your file.

Code:
Dim objFSO, objFile
Set objFSO = CreateObject("scripting.filesystemobject")

Set objFile = objFSO.GetFile("[red]C:\Users\mmaclachlan\Desktop\GPResult.txt[/red]")

WScript.Echo "Minutes since last modified: " & DateDiff("N",objFile.DateLastModified, Now)
WScript.Echo "Last Modified: " & objFile.DateLastModified
WScript.Echo "Last Accessed: " & objFile.DateLastAccessed

Run it once on an old file. Then open the file and run again. Note that the DateLastAccessed does not change. Then modify the file and run again and see that the while the last accessed date does not change the DateLastModified does.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
I ended up with this in case anyone searches this in the future. That function at the end is from msdn. It turns out that objFile.LastAccessed produces this weird string that doesnt have the same format as Now and wont work with DateDiff. So I had reformat it.

I also had to run it with cscript or wscript.echo would produce pop-ups. I needed this as a command line script.

' ------ SCRIPT CONFIGURATION ------
strFilePath = "c:\\test.txt"
strComputer = "."
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set objFile = objWMI.Get("CIM_Datafile=""" & strFilePath & """")

dim temp
dim diff

temp = WMIDateStringToDate(objFile.LastAccessed)
diff = DateDiff("n",temp,now)
WScript.Echo "Minutes since last Access: " & diff


If diff < 21 Then
wscript.echo("Accessed in last 20 minutes!")
else
wscript.echo("Not accessed in last 20 mins")
end if

'runlevel exit code for external program

If diff < 21 Then
wscript.quit(1)
else
wscript.quit(0)
end if


Function WMIDateStringToDate(dtmDate)
WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
& " " & Mid (dtmDate, 9, 2) & ":" & _
Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate, _
13, 2))
End Function
 
you can also use the script of markdmac with DateStringToDate function :

Code:
Dim objFSO, objFile
Set objFSO = CreateObject("scripting.filesystemobject")

Set objFile = objFSO.GetFile("D:\IIS\consultime_vbs\cvVerif.wsf")

WScript.Echo DateStringToDate(objFile.DateLastModified, "dmy") & vbcrlf & Now
WScript.Echo "Minutes since last modified: " & DateDiff("N", DateStringToDate(objFile.DateLastModified, "dmy"), Now)
set objFile = nothing
Set objFSO = nothing

'd : date string
'dateformat : to handle both format "month/day/year" and day/month/year" representation
function DateStringToDate(d, dateformat)
    dim arr, dat, tim, dat1
    
    'WScript.Echo d, dateformat
    arr = split(d, " ")
    dat = split(arr(0), "/")
    tim = split(arr(1), ":")
    if lcase(dateformat) = "mdy" then
        dat1 = dateserial(dat(2), dat(0), dat(1))
    else
        dat1 = dateserial(dat(2), dat(1), dat(0))
    end if
    dat1 = dateadd("h", tim(0), dat1)
    dat1 = dateadd("n", tim(1), dat1)
    dat1 = dateadd("s", tim(2), dat1)
    if ubound(arr) = 2 then
        if lcase(arr(3)) = "pm" then
            dat1 = dateadd("h", 12, dat1)
        end if
    end if
    DateStringToDate = dat1
end function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top