This script creates a new Excel Workbook and Spreadsheet
for the Drive, File Extension and Beginning DateTime
(LastModified) the user specifies.
The new Excel document is placed in the users "My Documents" directory by default.
This script ran relatively quick until I added the abililty to get the file Owner.
Anyone have any ideas on how to put some life back into this script??
Thanks Tsuji for the code that the DateToWMIDate function was written from.
Thanks
John Fuhrman
faq329-6766
faq329-7301
thread329-1334328
thread329-1424438
for the Drive, File Extension and Beginning DateTime
(LastModified) the user specifies.
The new Excel document is placed in the users "My Documents" directory by default.
This script ran relatively quick until I added the abililty to get the file Owner.
Anyone have any ideas on how to put some life back into this script??
Thanks Tsuji for the code that the DateToWMIDate function was written from.
Code:
'==========================================================================
'
' NAME: List_Apps_On_Drive (prompts for drive letter and file extension).vbs
'
' AUTHOR: John F. Fuhrman III
' DATE : 1/22/2010
'
' COMMENT: This script creates a new Excel Workbook and Spreadsheet
' for the Drive and File Extension the user specifies.
' The new Excel document is placed in the users "My Documents"
' directory by default.
'
'==========================================================================
strComputer = "." ' use "." for local computer
strDrive=UCase(InPutBox("Enter Drive Letter", "Search Drive", "O"))
strExtension=UCase(InPutBox("Enter File Name Extension to search for.", _
"File Extension Search", "MDB"))
strDateInput=InPutBox("Enter beginning date for Search" & VBcrlf & _
"Ending Date will be " & Date() & " 12:00 AM", _
"Date Range Search", datevalue(dateadd("m",-3,Now())) _
& " 12:00 AM")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService. _
ExecQuery("Select * from CIM_DataFile" _
& " where Drive=" & "'" & strDrive & ":'" _
& " AND Extension='" & strExtension & "'" _
& " AND LastModified>'" & ConvDateTimeWMI(strDateInput)& "'")
' WScript.Echo "# of files found: " & colFiles.Count
'
' Set colFiles = objWMIService. _
' ExecQuery("Select * from CIM_DataFile" _
' & " where Drive=" & "'" & strDrive & ":' and Extension='" &_
' strExtension & "'")
' WScript.Echo "# of files found: " & colFiles.Count
' Spreadsheet file to be created.
strExcelPath = strExtension & "-File_Listing_for_Drive_" & strDrive & ".xls"
' Wscript.Echo strExcelPath
' Bind to Excel object.
On Error Resume Next
Set objExcel = CreateObject("Excel.Application")
If Err.Number <> 0 Then
On Error GoTo 0
Wscript.Echo "Excel application not found."
Wscript.Quit
End If
On Error GoTo 0
'Set this True if you would like to see the Spreadsheet being updated.
objExcel.Visible = True
'Create a new workbook
objExcel.Workbooks.Add
' Bind to worksheet.
Set objWorksheet = objExcel.ActiveWorkbook.Worksheets(1)
objWorksheet.Name = strExtension & "-File Listing for Drive " & strDrive
' Populate spreadsheet cells with user attributes.
objWorksheet.Cells(1,1).Value = "Access Application Name" 'Cell A1
objWorksheet.Cells(1,2).Value = "Drive" 'Cell B1
objWorksheet.Cells(1,3).Value = "Location" 'Cell C1
objWorksheet.Cells(1,4).Value = "File Size" 'Cell D1
objWorksheet.Cells(1,5).Value = "Creation Date" 'Cell E1
objWorksheet.Cells(1,6).Value = "Last Modified" 'Cell F1
objWorksheet.Cells(1,7).Value = "File Owner " 'Cell G1
objWorksheet.Cells(1,8).Value = "Point of Contact" 'Cell H1
objWorksheet.Cells(1,9).Value = "POC Contact Number" 'Cell I1
objWorksheet.Cells(1,10).Value = "File Status" 'Cell J1
'Set Column Headings to Bold
objWorksheet.Range("A1:Z1").Font.Bold = True
'Defign Worksheet Ranges for Sorting and column sizing.
Set objRange = objWorksheet.UsedRange
Set objRange2 = objExcel.Range("B1") 'Sort by Drive'
Set objRange3 = objExcel.Range("C1") 'Sort by Path'
Set objRange4 = objExcel.Range("D1") 'Sort by Size'
'Autofit all columns
objRange.EntireColumn.Autofit()
k = 2
For Each objFile in colFiles
objWorksheet.Cells(k,1).Value = objFile.FileName 'A
objWorksheet.Cells(k,2).Value = objFile.Drive 'B
objWorksheet.Cells(k,3).Value = objFile.Path 'C
objWorksheet.Cells(k,4).Value = objFile.FileSize 'D
objWorksheet.Cells(k,5).Value = ConvWMITime(objFile.CreationDate) 'E
objWorksheet.Cells(k,6).Value = ConvWMITime(objFile.LastModified) 'G
If GetOwner(objFile.Name) = NULL Then 'H
objWorksheet.Cells(k,7).Value = "Unknown"
ElseIf GetOwner(objFile.Name) = "" Then
objWorksheet.Cells(k,7).Value = "Unknown"
Else
objWorksheet.Cells(k,7).Value = GetOwner(objFile.Name)
End If
'' Uncomment for Testing filling in of worksheet
' Wscript.echo k
' if k >=7 then
'' objExcel.ActiveWorkbook.SaveAs strExcelPath
' objExcel.ActiveWorkbook.Close
' wscript.quit
' End if
k = k + 1
Next
' Format the spreadsheet.
Const xlAscending = 1
Const xlDescending = 2
Const xlYes = 1
'Autofit all columns
objRange.EntireColumn.Autofit()
'Set srting order
'objRange.Sort objRange2, xlAscending, , , , , , xlYes
objRange.Sort objRange3, xlAscending, , , , , , xlYes
'objRange.Sort objRange4, xlAscending, , , , , , xlYes
' Save the spreadsheet and close the workbook.
objExcel.ActiveWorkbook.SaveAs strExcelPath
objExcel.ActiveWorkbook.Close
' Quit Excel.
objExcel.Application.Quit
'=============================================================================
' Section for Functions
'=============================================================================
'************************************************************************************
' Convert WMI Time Function
'************************************************************************************
Function ConvWMITime(wmiTime)
On Error Resume Next
yr = left(wmiTime,4)
mo = mid(wmiTime,5,2)
dy = mid(wmiTime,7,2)
tm = mid(wmiTime,9,6)
ConvWMITime = mo & "/" & dy & "/" & yr & " " & FormatDateTime(left(tm,2) & _
":" & Mid(tm,3,2) & ":" & Right(tm,2),3)
End Function
'EOF
'************************************************************************************
' Convert WMI Time Function
'************************************************************************************
Function dtConvert(strDateTime)
' Convert File DateTime Stamp to a readable format
strConvertDT = CDate(Mid(strDateTime, 5, 2) & "/" &_
Mid(strDateTime, 7, 2) & _
"/" & Left(strDateTime, 4) & " " &_
Mid (strDateTime, 9, 2) & ":" & _
Mid(strDateTime, 11, 2) & ":" & _
Mid(strDateTime, 13, 2))
dtConvert = strConvertDT
End Function
'EOF
'************************************************************************************
' Get File Attribute "Owner"
'************************************************************************************
Function GetOwner(strFile)
strComputer = "."
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
On Error Resume Next
Set objCollection = objWMI.ExecQuery _
("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & strFile _
& "'} WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")
For Each objSID in objCollection
GetOwner = objSID.AccountName
Next
If VarType(GetOwner) = 0 Then
GetOwner = "error"
End If
End Function
'EOF
'************************************************************************************
' Convert Standard DateTime to WMI DateTime Function
'************************************************************************************
Function ConvDateTimeWMI(strDateTime)
set oswbemdtm=createobject("wbemscripting.swbemdatetime")
oswbemdtm.setvardate strDateTime,true 'CONVERT_TO_LOCAL_TIME=true
ConvDateTimeWMI = oswbemdtm
set oswbemdtm = Nothing
End Function
'EOF
'=============================================================================
' Section for SubRoutines
'=============================================================================
Thanks
John Fuhrman
faq329-6766
faq329-7301
thread329-1334328
thread329-1424438