Solved

Error Handling in VB Script

Posted on 2006-11-29
11
2,325 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:cstraim
  • 5
  • 5
11 Comments
 
LVL 12

Expert Comment

by:jkaios
ID: 18040512
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
 

Author Comment

by:cstraim
ID: 18045577
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
 
LVL 8

Expert Comment

by:jwarnken
ID: 18054020
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
 

Author Comment

by:cstraim
ID: 18055267
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
 
LVL 8

Expert Comment

by:jwarnken
ID: 18055459
replace Set oExec    = WshShell.Exec("%comspec% /c dire")
with Set oExec    = WshShell.Exec("G:\subgroup\access data\lBX\backup.bat")
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:cstraim
ID: 18055713
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
 
LVL 8

Expert Comment

by:jwarnken
ID: 18055822
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
 

Author Comment

by:cstraim
ID: 18055987
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
 
LVL 8

Accepted Solution

by:
jwarnken earned 500 total points
ID: 18056037
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
 

Author Comment

by:cstraim
ID: 18056641
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
 
LVL 8

Expert Comment

by:jwarnken
ID: 18056663
Thanks for the catch.
Glad you were able to get it working
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now