Solved

Error Handling in VB Script

Posted on 2006-11-29
11
2,332 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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
 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

786 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