I am trying to author a script that will return (among other things) the "Last Accessed Date" for subfolders of a given folder (nonrecursively).
The problem is that when running the script, all subfolders change their LAD to the current date and time... rendering the information useless.
Is there a way to avoid this? Code below if it helps...
Code:
strComputer = "."
Const MY_COMPUTER = &H11&
Const ForWriting = 2
Const WINDOW_HANDLE = 0
Const OPTIONS = 0
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Delete HTM if it is already present
If objFSO.FileExists("C:\SubfolderInfo.htm") Then
objFSO.DeleteFile("C:\SubfolderInfo.htm")
End If
'Create popup window prompting user to select a drive or folder
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_COMPUTER)
Set objFolderItem = objFolder.Self
strPath2 = objFolderItem.Path
Set objShell = CreateObject("Shell.Application")
Set objFolderSelect = objShell.BrowseForFolder _
(WINDOW_HANDLE, "Select a folder:", OPTIONS, strPath2)
If objFolderSelect Is Nothing Then
Wscript.Quit
End If
Set objFolderItem = objFolderSelect.Self
strPath = objFolderItem.Path
Set colSubfolders = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strPath & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")
'Begin HTML file creation
Set objOutFile = objFSO.CreateTextFile("C:\SubfolderInfo.htm", ForWriting, True)
objOutFile.writeline "<html>"
objOutFile.writeline "<head>"
objOutFile.writeline "<title>Subfolder Information for " & strPath & "</title>"
objOutFile.writeline "</head>"
objOutFile.writeline "<body>"
objOutFile.writeline "<h1>Subfolder Information for " & strPath & "</h1>" & vbCrLf
objOutFile.writeline "<h2>" & Now() & "</h2>" & vbCrLf
objOutFile.writeline "<table border=1 cellpadding=6>" & vbCrLf
objOutFile.writeline "<tr>" & vbCrLf
objOutFile.writeline "<b><td>Folder Name</td><td>Size in MB</td><td>Last Modified</td>" & _
"<td>Last Accessed</td></b></tr>" & vbCrLf
'Add rows and data for each subfolder
For Each objFolder in colSubfolders
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
Set objFolder2 = objFSO2.GetFolder(objFolder.Name)
intSizeInMB = objFolder2.Size / 1084576
intSizeInMB = Round(intSizeInMB, 1)
objOutFile.writeline "<tr>" & vbCrLf
objOutFile.writeline "<td>" & objFolder2.Name & "</td><td align=" & chr(34) & "right" & _
chr(34) & ">" & intSizeInMB & "</td><td>" & objFolder2.DateLastModified & "</td><td>" & _
objFolder2.DateLastAccessed & "</td>" & vbCrLf
objOutFile.writeline "</tr>" & vbCrLf
Next
'Complete HTML file creation
objOutFile.writeline "</table>"
objOutFile.writeline "</body>"
objOutFile.writeline "</html>"
'Removed confirmation box
'MsgBox "Your results have been catalogued in C:\SubfolderInfo.htm",4096,"Scan Complete..."
'Display HTML file
Set objShell2 = Wscript.CreateObject("Wscript.Shell")
objShell2.Run "C:\SubfolderInfo.htm"