[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Error Handling in VB Script

Posted on 2006-11-29
11
Medium Priority
?
2,352 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

649 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