Hello All,
I've written a little scriptlet that recurses folders for certain file extensions etc but am a little concerned about the error handling....
Can someone point me in the right direction for better handling...??
If I try & put in objfolder.path into the error statement to see the folder it doesn't work, also I had to put each error print on a new line as more than 1 on the same line didn't work for some reason.
I'm sure I'm just missing something simple but I'm a bit scripted out today...
Any advice appreciated
Here's the script >>>
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("Wscript.Shell")
If (right(Ucase(WScript.FullName),11)="WSCRIPT.EXE") Then
WshShell.Run WshShell.ExpandEnvironmentStrings("%COMSPEC%") & " /c cscript.exe """ & Wscript.ScriptFullName & """"
Wscript.Quit
End If
SearchExtn = InputBox("Please Type File Extension To Search For: ","File Type To Find?")
oAnswer = InputBox("Please Type Drive Letter Only as Follows: " & chr(34) & "C" & Chr(34),"Which Drive Would You Like To Scan?")
If oAnswer = "" Then
MsgBox "You Have Not Entered A Drive Letter... Quitting...."
Wscript.Quit
End If
oDrive = Left(oAnswer,1)
strFolder = oAnswer & ":\"
Set objFolder = objFSO.GetFolder(strFolder)
MyCur = WshShell.CurrentDirectory
Set oLogFile = objFSO.CreateTextFile(MyCur & "\Folders_Scanned_From_" & oDrive & "_.Log",2)
Set oFoundFiles = objFSO.CreateTextFile(MyCur & "\Files_Found_From_" & oDrive & "_.txt",2)
'WScript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile In colFiles
If right(objFile.Name,3) = SearchExtn Then
FileSize = SetBytes(objfile.size)
'oWrite "FileName: " & objfile.path & VbCrLf & "Size: " & FileSize & VbCrLf & "Last Accessed: " & objfile.datelastaccessed & VbCrLf
oWriteFound "FileName: " & objfile.path & VbCrLf & "Size: " & FileSize & VbCrLf & "Last Accessed: " & objfile.datelastaccessed & VbCrLf
end if
Next
ShowSubFolders(objFolder)
Sub ShowSubFolders(objFolder)
On Error Resume Next
oWrite "Scanning Folder: " & objFolder.Path
If Err Then
oWrite "Error Detected! ********************************************************************* "
oWrite Err.Description
oWrite Err.Number
Err.Clear
'objFolder = objFolder + 1
On Error Goto 0
End If
Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders
'WScript.Echo objSubFolder.Path
Set colFiles = objSubFolder.Files
For Each objFile In colFiles
If right(objFile.Name,3) = SearchExtn Then
FileSize = SetBytes(objfile.size)
'oWrite "FileName: " & objfile.path & VbCrLf & "Size: " & FileSize & VbCrLf & "Last Accessed: " & objfile.datelastaccessed & VbCrLf
oWriteFound "FileName: " & objfile.path & VbCrLf & "Size: " & FileSize & VbCrLf & "Last Accessed: " & objfile.datelastaccessed & VbCrLf
end if
Next
ShowSubFolders(objSubFolder)
Next
End Sub
Function oWrite(strMessage)
Wscript.Echo (strMessage)
oLogFile.WriteLine (strMessage)
End Function
Function oWriteFound(strMessage)
oFoundFiles.WriteLine (strMessage)
End Function
Function SetBytes(Bytes)
If Bytes >= 1073741824 Then
SetBytes = Round(FormatNumber(Bytes / 1024 / 1024 / 1024, 2), 0) & " GB"
ElseIf Bytes >= 1048576 Then
SetBytes = Round(FormatNumber(Bytes / 1024 / 1024, 2), 0) & " MB"
ElseIf Bytes >= 1024 Then
SetBytes = Round(FormatNumber(Bytes / 1024, 2), 0) & " KB"
ElseIf Bytes < 1024 Then
SetBytes = Bytes & " Bytes"
Else
SetBytes = "0 Bytes"
End If
End Function
oLogFile.Close
MsgBox "Complete!"
Cheers
Woolsdog
P.S. I did it this way as on some systems, CIM_DataFile doesn't seem to work before anyone suggests that as an alternative..
Keyboard Not Detected.....
Press F1 to Continue.
:}
I've written a little scriptlet that recurses folders for certain file extensions etc but am a little concerned about the error handling....
Can someone point me in the right direction for better handling...??
If I try & put in objfolder.path into the error statement to see the folder it doesn't work, also I had to put each error print on a new line as more than 1 on the same line didn't work for some reason.
I'm sure I'm just missing something simple but I'm a bit scripted out today...
Any advice appreciated
Here's the script >>>
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("Wscript.Shell")
If (right(Ucase(WScript.FullName),11)="WSCRIPT.EXE") Then
WshShell.Run WshShell.ExpandEnvironmentStrings("%COMSPEC%") & " /c cscript.exe """ & Wscript.ScriptFullName & """"
Wscript.Quit
End If
SearchExtn = InputBox("Please Type File Extension To Search For: ","File Type To Find?")
oAnswer = InputBox("Please Type Drive Letter Only as Follows: " & chr(34) & "C" & Chr(34),"Which Drive Would You Like To Scan?")
If oAnswer = "" Then
MsgBox "You Have Not Entered A Drive Letter... Quitting...."
Wscript.Quit
End If
oDrive = Left(oAnswer,1)
strFolder = oAnswer & ":\"
Set objFolder = objFSO.GetFolder(strFolder)
MyCur = WshShell.CurrentDirectory
Set oLogFile = objFSO.CreateTextFile(MyCur & "\Folders_Scanned_From_" & oDrive & "_.Log",2)
Set oFoundFiles = objFSO.CreateTextFile(MyCur & "\Files_Found_From_" & oDrive & "_.txt",2)
'WScript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile In colFiles
If right(objFile.Name,3) = SearchExtn Then
FileSize = SetBytes(objfile.size)
'oWrite "FileName: " & objfile.path & VbCrLf & "Size: " & FileSize & VbCrLf & "Last Accessed: " & objfile.datelastaccessed & VbCrLf
oWriteFound "FileName: " & objfile.path & VbCrLf & "Size: " & FileSize & VbCrLf & "Last Accessed: " & objfile.datelastaccessed & VbCrLf
end if
Next
ShowSubFolders(objFolder)
Sub ShowSubFolders(objFolder)
On Error Resume Next
oWrite "Scanning Folder: " & objFolder.Path
If Err Then
oWrite "Error Detected! ********************************************************************* "
oWrite Err.Description
oWrite Err.Number
Err.Clear
'objFolder = objFolder + 1
On Error Goto 0
End If
Set colFolders = objFolder.SubFolders
For Each objSubFolder In colFolders
'WScript.Echo objSubFolder.Path
Set colFiles = objSubFolder.Files
For Each objFile In colFiles
If right(objFile.Name,3) = SearchExtn Then
FileSize = SetBytes(objfile.size)
'oWrite "FileName: " & objfile.path & VbCrLf & "Size: " & FileSize & VbCrLf & "Last Accessed: " & objfile.datelastaccessed & VbCrLf
oWriteFound "FileName: " & objfile.path & VbCrLf & "Size: " & FileSize & VbCrLf & "Last Accessed: " & objfile.datelastaccessed & VbCrLf
end if
Next
ShowSubFolders(objSubFolder)
Next
End Sub
Function oWrite(strMessage)
Wscript.Echo (strMessage)
oLogFile.WriteLine (strMessage)
End Function
Function oWriteFound(strMessage)
oFoundFiles.WriteLine (strMessage)
End Function
Function SetBytes(Bytes)
If Bytes >= 1073741824 Then
SetBytes = Round(FormatNumber(Bytes / 1024 / 1024 / 1024, 2), 0) & " GB"
ElseIf Bytes >= 1048576 Then
SetBytes = Round(FormatNumber(Bytes / 1024 / 1024, 2), 0) & " MB"
ElseIf Bytes >= 1024 Then
SetBytes = Round(FormatNumber(Bytes / 1024, 2), 0) & " KB"
ElseIf Bytes < 1024 Then
SetBytes = Bytes & " Bytes"
Else
SetBytes = "0 Bytes"
End If
End Function
oLogFile.Close
MsgBox "Complete!"
Cheers
Woolsdog
P.S. I did it this way as on some systems, CIM_DataFile doesn't seem to work before anyone suggests that as an alternative..
Keyboard Not Detected.....
Press F1 to Continue.
:}