Error Handling in VB Script

Hello All

I am trying to put error handling into my code below. I only need to check to see if a batch file was successfully run, and if so, to continue with the rest of the code:

Set WshShell = WScript.CreateObject("WScript.Shell")
WSHShell.run("\\server\folder\setup.bat",7,true)

'I NEED THE ERROR HANDLING HERE, NOT SURE HOW TO DO THIS. JUST WANT TO MAKE SURE THAT THE BATCHFILE WAS SUCCESSFULLY RUN.  (THE BATCH FILE IS ALSO BELOW THE CODE SO YOU CAN SEE THAT AS WELL)

Const cDatabaseToOpen = "G:\Subgroup\Access Data\LBX\current LBX.mdb"
On Error Resume Next
Dim AcApp
Set AcApp = CreateObject("Access.Application")
If AcApp.Version >= 10 Then
     AcApp.AutomationSecurity = 1 ' msoAutomationSecurityLow
End If
AcApp.Visible = false
AcApp.OpenCurrentDatabase cDatabaseToOpen
acapp.docmd.runmacro "download_dspec"
acapp.docmd.runmacro "download_aspec"
acapp.docmd.runmacro "download_ospec"
If AcApp.CurrentProject.FullName <> "" Then
     AcApp.UserControl = false
Else
     AcApp.Quit
     MsgBox "Failed to open '" & cDatabaseToOpen  & "'."
End If


'HERE IS THE BATCH FILE CODE
'md "\%date:/=-%"
'xcopy c:\test c:\"\%date:/=-%" /e
cstraimAsked:
Who is Participating?
 
jwarnkenConnect With a Mentor Commented:
not a problem, This will log to a file and keep appending to the same file each time

Const ForWriting = 2
Const ForReading = 1
Const ForAppending = 8
Const TristateFalse = 0
Dim WshShell, oExec.fso
Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set oExec    = WshShell.Exec("G:\subgroup\access data\lBX\backuptest.bat")

     If Not oExec.StdOut.AtEndOfStream Then
          Log2File(oExec.StdOut.ReadAll)
     End If

     If Not oExec.StdErr.AtEndOfStream Then
          Log2File("STDERR: " + oExec.StdErr.ReadAll)
     End If

Sub Log2File(msg)
      Set outfile = fso.OpenTextFile ("c:\logs\yourlog.log", ForAppending, True)
      outfile.Writeline (msg)
      outfile.close
End Sub
0
 
jkaiosIT DirectorCommented:
Since you specify the "bWaitOnReturn" parameter (the 3rd parameter) of the Run method as TRUE, then your script should run the batch file and "wait" until the batch file execution is finished and then return any error code if it encounters any.  Therefore, what you simply need to do is check the return value of the WSHShell.Run method as follows:

iRetCode = WSHShell.run("\\server\folder\setup.bat",7,true)

If (iRetCode <> 0) Then
   MsgBox "Some error occurred - error number: " & iRetCode
   Wscript.Quit
If
0
 
cstraimAuthor Commented:
OK I actually didnt try the code i put together to start off with, so with or without the error handling I'm getting an error from the windows Script host

Line:2
Char:1
Error:Unable to wait for process
Code:80020009
Source:WshShell.run

Here is the code

Set WshShell = WScript.CreateObject("WScript.Shell")
iRetCode = WSHShell.run("G:\subgroup\access data\lBX\backup.bat",7,true)

If (iRetCode <> 0) Then
   MsgBox "Backup Failed" & iRetCode
   Wscript.Quit
End If

Const cDatabaseToOpen = "G:\Subgroup\Access Data\LBX\current LBX.mdb"
On Error Resume Next
Dim AcApp
Set AcApp = CreateObject("Access.Application")
If AcApp.Version >= 10 Then
     AcApp.AutomationSecurity = 1 ' msoAutomationSecurityLow
End If
AcApp.Visible = false
AcApp.OpenCurrentDatabase cDatabaseToOpen
acapp.docmd.runmacro "download_dspec"
acapp.docmd.runmacro "download_aspec"
acapp.docmd.runmacro "download_ospec"
If AcApp.CurrentProject.FullName <> "" Then
     AcApp.UserControl = false
Else
     AcApp.Quit
     MsgBox "Failed to open '" & cDatabaseToOpen  & "'."
End If

Anyone know what I'm doin wrong?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
jwarnkenCommented:
You can try using Exec rather than run then you will be able to read the StdErr

Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec    = WshShell.Exec("%comspec% /c dire")

Function ReadAllFromAny(oExec)

     If Not oExec.StdOut.AtEndOfStream Then
          ReadAllFromAny = oExec.StdOut.ReadAll
          Exit Function
     End If

     If Not oExec.StdErr.AtEndOfStream Then
          ReadAllFromAny = "STDERR: " + oExec.StdErr.ReadAll
          Exit Function
     End If
     
     ReadAllFromAny = -1
End Function
0
 
cstraimAuthor Commented:
Hello Jwarnken

I am not sure what to do to run the batch file with this new code.  I need to run the following batch file:
 G:\subgroup\access data\lBX\backup.bat

Where do i put this in the code you gave me?

Thanks again :)
0
 
jwarnkenCommented:
replace Set oExec    = WshShell.Exec("%comspec% /c dire")
with Set oExec    = WshShell.Exec("G:\subgroup\access data\lBX\backup.bat")
0
 
cstraimAuthor Commented:
I am using the VBS code as you suggested:

Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec    = WshShell.Exec("G:\subgroup\access data\lBX\backuptest.bat")

Function ReadAllFromAny(oExec)

     If Not oExec.StdOut.AtEndOfStream Then
          ReadAllFromAny = oExec.StdOut.ReadAll
          Exit Function
     End If

     If Not oExec.StdErr.AtEndOfStream Then
          ReadAllFromAny = "STDERR: " + oExec.StdErr.ReadAll
          Exit Function
     End If
     
     ReadAllFromAny = -1
End Function

Now when I run it, the command window pops up for a second but nothing else happens, it dissappears.

Here is the batch file I am trying to run (which runs fine on it's own)

md "G:\Subgroup\Access Data\LBX\test\""\%date:/=-%"
xcopy "G:\Subgroup\Access Data\LBX\Raw Data\Current Week" "G:\Subgroup\Access Data\LBX\test\""\%date:/=-%" /e
xcopy "G:\Subgroup\Access Data\LBX\current LBX.mdb" "G:\Subgroup\Access Data\LBX\test\""\%date:/=-%"
0
 
jwarnkenCommented:
try this
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec    = WshShell.Exec("G:\subgroup\access data\lBX\backuptest.bat")

      If Not oExec.StdOut.AtEndOfStream Then
          Wscript.echo oExec.StdOut.ReadAll
      End If

      If Not oExec.StdErr.AtEndOfStream Then
          Wscript.echo "STDERR: " + oExec.StdErr.ReadAll
      End If
you should see the output of the batch or the error
sorry the ealier script was an example out of the ms documentation so i did not test it but this works for me
0
 
cstraimAuthor Commented:
That is awesome.  Thank you it works now.  Can you tell me how to get rid of the windows script window at the end, and have it save to a log file instead without an user intervention necessary.  This is supposed to run at night through task scheduler..  I can open another question if you'd like so I can give you addtional points.  I really appreciate your help.
0
 
cstraimAuthor Commented:
THANK YOU SOO MUCH FOR YOUR HELP BUDDY.  I just changed your code a bit, because there was a period instead of a comma in the DIM statement. If anyone needs to have a way to backup data into a new folder with "today's" date and have error handling built in, this is the code for you :)


Const ForWriting = 2
Const ForReading = 1
Const ForAppending = 8
Const TristateFalse = 0
Dim WshShell, oExec,  fso
Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set oExec    = WshShell.Exec("G:\subgroup\access data\lBX\backuptest.bat")

     If Not oExec.StdOut.AtEndOfStream Then
          Log2File(oExec.StdOut.ReadAll)
     End If

     If Not oExec.StdErr.AtEndOfStream Then
          Log2File("STDERR: " + oExec.StdErr.ReadAll)
     End If

Sub Log2File(msg)
     Set outfile = fso.OpenTextFile ("c:\logs\yourlog.log", ForAppending, True)
     outfile.Writeline (msg)
     outfile.close
End Sub
0
 
jwarnkenCommented:
Thanks for the catch.
Glad you were able to get it working
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.