1
votes

Looking for a little help with working on error handling in VBscript.

What I am trying to do is tell the script that if an error occurs during one of the commands that it should re run the script. Basically I am just zipping some files and copying them over to a shared drive, then deleting the original copies once the copy is complete. Now if an error occurs in the zip or copy process I don't want the rest of the script running and having it delete the files. So I just need the script to start over.

At the moment all I have it doing is printing out an echo that says "An error has occurred re-run script" but this is going to be done as a scheduled task so I really need it to be automated. I am just not sure how the syntax would work for this or if it is even possible.

I have seen "On Error Resume Next" but not sure how that plays into how I should be writing this. I don't think I Should be using it because I do not want the script to continue if an error occurs I just want it to start from the beginning again.

This is what it looks like at the moment:

Command2 = "Robocopy.exe " & sLocalDestinationPath & " " & sFinalDestinationPath 

        RetVal = Shell.Run(Command2,0,true)


        If err.Number <> 0 Then
        'WScript.Echo "An error has occured copying the files, re-run Script."
        End If

If I am going at this problem the wrong way and there is something more efficient I should be doing I would appreciate the help greatly. I am new to VBscript and I am still getting used to it.

1
if an error occurs during one of the commands that it should re run the script wouldn't that cause an endless loop? Nothing has changed, so the error would occur again, which would cause the script to start again, which would cause the error again, which would cause the script to start again, and on and on and on ...JimmyPena
Yes, this is something that I thought of but, if the error was something like the files having not finished being zipped...well...hmm I don't honestly really know what I'm trying to go for here. Basically I just need to make sure that the files are zipped and copied without error. And I can't really think of a way to do it without getting very complicated.parchambeau
The files are local, right? Copying the files to a network folder might be the point of failure. I would simply execute the script, checking for errors and if one occurs, log the error and stop. Set it to run as scheduled. Then check the log later to see if it failed at any point. I'm sure a lot of organizations do it like this.JimmyPena
Tip unrelated to the question: you'll want to wrap the paths in doublequotes whenever there is a change a space occurs in a path: Command2 = "Robocopy.exe """ & sLocalDestinationPath & """ """ & sFinalDestinationPath & """". A call like Robocopy.exe c:\my tests\test 1 c:\my results\result 1 will now become Robocopy.exe "c:\my tests\test 1" "c:\my results\result 1"AutomatedChaos

1 Answers

0
votes

May I suggest Eric Lippert's Blog...

Error Handling in VBScript, Part One

Error Handling in VBScript, Part Two

Error Handling in VBScript, Part Three

These resources recently helped me solve a nasty error handling problem.