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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recurse Folder Error Checking...

Status
Not open for further replies.

Woolers

Technical User
Jan 11, 2005
56
GB
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.
:}
 
Code:
If objFSO.FolderExists(strFolder) Then
    Set objFolder = objFSO.GetFolder(strFolder)
Else
    wscript.echo "Folder does not exist, quiting"
    wscript.quit
End If

You should also verify that the drive input is in fact a letter and not a number or other character. Here is a sample.

Code:
'==========================================================================
'
' NAME: CheckNumberOrAlpha.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.TheSpidersParlor.com[/URL]
' COPYRIGHT (c) 2006 All Rights Reserved
' DATE  : 6/13/2006
'
' COMMENT: Checks if a character is a number or alpha character
'
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS 
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'
'==========================================================================

string1 = "99LuftBaloons"
string2 = "Text string"

WScript.Echo CheckLeft(string1)
WScript.Echo CheckLeft(string2)


Function CheckLeft(stringtocheck)

If IsNumeric(Left(stringtocheck,1)) Then
	CheckLeft = "Number"
ElseIf Asc(left(stringtocheck,1)) >64 And Asc(left(stringtocheck,1))<91 _ 
	Or Asc(left(stringtocheck,1)) >96 And Asc(left(stringtocheck,1))<123 Then
   CheckLeft = "Alpha"
End If
End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Thanks Mark,

I note the error checking there is if the folder exists, but I was more interested in getting the details of a specific error, if the folder doesn't exist the script will just carry on, I was thinking more along the lines of an access denied error.

Just really wanted to double check that my error handling in the script was ok ?

Cheers

Keyboard Not Detected.....
Press F1 to Continue.
:}
 
You can use:

Code:
' Report any errors
If Err.Number <> 0 Then
   Wscript.Echo Err.Number & " " & Err.Description
   'Clear the error for continued error detection
   Err.Clear
End If

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top