Wednesday, November 15, 2006

Last Accessed VBS Script

'This script is to find the date/time last accessed of files
'Version 1.0

Set fso = CreateObject ("Scripting.FileSystemObject")

If WScript.Arguments.Count = 0 then
RecurseDirectory "."
Else
RecurseDirectory WScript.Arguments.Item(0)
End If

Sub Recursedirectory(strDir)
Set refFolder = fso.GetFolder(strDir)

'Process files in directory
For Each refFile in refFolder.Files
WScript.Echo """" & refFile.Path & """" & "," & _
"""" & (refFile.DateCreated) & """" & "," & _
"""" & (refFile.DateLastModified) & """" & "," & _
"""" & (refFile.DateLastAccessed) & """" & "," & _
"""" & (refFile.size) & """" & ","
Next

'Then Recurse down directory tree
For Each refSubFolder in refFolder.SubFolders
RecurseDirectory refSubFolder.Path
Next
end sub

'To run:
'cscript //nologo DateLastAccessed.vbs c:\ >> DateLastAccessed.csv


------------------------------------------------------------------------------------

Getting KB instead of bytes is as simple as dividing by 1024.  If you want to round up to the nearest KB, add 1023 to the returned value then divide by 1024 and then use the INT function to get the size in KB.  i.e. SizeKB = int((refFile.Size+1023)/1024)

To get the date in a different format you have many options.  One of the easiest would be to use the SPLIT function to, duh, split it into pieces and then glue them back together.  Assuming the refFile.DateLastAccessed is

10/24/2006 8:22:42 AM

aryDate = Split(refFile.DateLastAccessed)    ' aryDate(0) = "10/24/2006", aryDate(1) = "8:22:42", aryDate(2) = "AM"
strDate = aryDate(0)    ' =12/24/2006
strTime = aryDate(1) & " " & aryDate(2)    ' =8:22:42 AM

Another way would be to use the various date manipulation functions, in particular DATEPART:

year = datepart("yyyy",refFile.DateLastAccessed)
month = datepart("m",refFile.DateLastAccessed)
day = datepart("d",refFile.DateLastAccessed)
hour = datepart("h",refFile.DateLastAccessed)
min = datepart("n",refFile.DateLastAccessed)
sec = datepart("s",refFile.DateLastAccessed)

theDate = year & month & day
theTime = hour & ":" min & ":" & sec

1 comment:

Anonymous said...

thanks, this was very useful
-anonymous coward.