0

I have combined some of my codes into the below:

    Option Explicit

    Dim oShell : Set oShell = WScript.CreateObject ("WScript.Shell")
    Dim FSO : set FSO = CreateObject("Scripting.FileSystemObject") 
    Dim StartTime,Elapsed 

    'msgBox(oShell.CurrentDirectory)
    'MsgBox(FSO.GetFile(Wscript.ScriptFullName).ParentFolder )
    oShell.CurrentDirectory = FSO.GetFile(Wscript.ScriptFullName).ParentFolder

    StartTime = Timer

    oShell.run "ParentChildLinkFinal.vbs", 1, True
    oShell.run "Parent_Child_Merge_final.vbs", 1, True
    oShell.run "CycleTime.vbs", 1, True
    oShell.run "Baddata.vbs", 1, True
    oShell.run "Matrixrefresh.vbs", 1, True

    Elapsed = Timer - StartTime

    MsgBox("Total time taken to finish this task:" & Elapsed & "in Seconds")

Now one concern here is- Suppose an error occurs in the any of the CycleTime.vbs file,but the control then going to the next Baddata.vbs file,without killing the Main.vbs file.So how to handle this,If error occurs stop the main.vbs there itself or let the execution to continue.

As Per Daniel

"IF NOT oShell.run( "CycleTime.vbs", 1, True) = 0 then Wscript.Quit would replace the line you are currently using to call "CycleTime.vbs" within Main.vbs

IF oShell.run( "CycleTime.vbs", 1, True) = 99 then Wscript.Quit Would also replace the line you are currently using to call "CycleTime.vbs". The difference is that you didn't specify how CycleTime.vbs could error out. If it could fail in such a way that it has a run-time error and simply stops executing, you would want to use the 1st option. If an error occurs, that you trap with error trapping, you can quit with a code. Like WScript.Quit 99. Then you could check to see if the script ended with that code via the 2nd version that I provided to you. It really comes down to how you want to handle the failure of your script that you are calling."

WHY SUCH APPROACH

@Daniel True helps the script to suspend its execution until the current .vbs finished. But I am looking for any special value if I can return a special value from each of the .vbs by which main.vbs will get to know that its current .vbs completed successfuly,let's start the next .vbs . But yes iff only success then the next would start. Currently I am seing that suppose an error occurs from any of the .vbs,then as soon as i closed the error window next script started to run. So I am trying atleast thinking iff any success full communication can be sent to the main.vbs. So is it possible?

CodeLover
  • 1,054
  • 6
  • 24
  • 40
  • @Hiten004 Will it be a good idea? Say if an error occurs `CycleTime.vbs` then, Can I catch it from the `Main.vbs`? – CodeLover Dec 31 '12 at 14:13
  • use return error code to Exist Main.vbs http://stackoverflow.com/questions/187040/how-do-i-return-an-exit-code-from-a-vbscript-console-application – Hiten004 Dec 31 '12 at 16:35

2 Answers2

2

To expand on what Hiten indicated, you would need to do some checking in your Main.vbs for the return value from your called .vbs files. You do this by utilizing the return value of Run.

For example:

Instead of doing oShell.run "CycleTime.vbs", 1, True do

IF NOT oShell.run( "CycleTime.vbs", 1, True) = 0 then Wscript.Quit

This will exit your current thread of execution if CycleTime.vbs halted due to a run-time error (the call to run the script will return false).

Now if you are able to handle errors gracefully within CycleTime.vbs, you can send a message when you close it by using Wscript.Quit code as explained in the other answer.

So if you want to handle errors gracefully in CycleTime.vbs, you can still indicate an error occurred. Simply check for the return code in your main script with something like this:

IF oShell.run( "CycleTime.vbs", 1, True) = 99 then Wscript.Quit

This way your script will end if CycleTime.vbs ends with code 99. This would happen if within CycleTime.vbs it had code like this:

If err then
    'Exit script with code 99 to indicate an error occurred.
    Wscript.Quit 99
End if
Daniel
  • 12,982
  • 3
  • 36
  • 60
  • I will upvote, because checking the return value of the .Run() *Function* call is the correct solution, but not before you remove the spuriuos () in the WScript.Quit *Sub* calls. – Ekkehard.Horner Dec 31 '12 at 22:28
  • @Ekkehard.Horner Sorry about that, thanks for the correction. :) – Daniel Dec 31 '12 at 22:34
  • @DanielCook for all the other .vbs file, how to write the overall code? as the one `cycletime.vbs` i used as an example.But the error can be arose from any of the .vbs file! – CodeLover Dec 31 '12 at 22:45
  • Just put the `WScript.Quit ##` in whatever script you want to check. Then in your main one check the exit code. `exitCode = oShell.Run(yourscript,1,true)` – Daniel Dec 31 '12 at 22:49
  • @DanielCook Can you edit my code as per your suggestion,which would help me to understand the fact quickly. Use @ myname when you write something so that i can get notifications! – CodeLover Dec 31 '12 at 22:58
  • 2
    @CodeLover I already showed you how to do it. You didn't provide enough code for someone to write the change for you. I also do not think that you should. I gave you enough that you should be able to figure it out on your own. – Daniel Dec 31 '12 at 23:14
  • @DanielCook my confusion is with the lines: `IF NOT oShell.run( "CycleTime.vbs", 1, True) = 0 then Wscript.Quit` and `IF oShell.run( "CycleTime.vbs", 1, True) = 99 then Wscript.Quit` . Are they both needed? How are they fulfilling the purposes? please help me to understand! – CodeLover Jan 01 '13 at 03:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21992/discussion-between-daniel-cook-and-codelover) – Daniel Jan 01 '13 at 14:30
  • @Ekkehard.Horner Just could you tell me what you meant by the line - `because checking the return value of the .Run() Function call is the correct solution, but not before you remove the spuriuos () in the WScript.Quit Sub calls.` – CodeLover Jan 02 '13 at 18:44
  • 2
    @CodeLover I meant (a) checking the return value of .Run (set by WScript.Quit Number in the called .vbs) is the way to go and (b) you must not use param list () when calling a Sub. – Ekkehard.Horner Jan 02 '13 at 18:57
1

Use vbscript error handling in main.vbs to stop the code and exit the main.vbs

for more detial click this link: VBScript -- Using error handling

Sample:

On Error Resume myErrCatch
'Do step 1
'Do step 2
'Do step 3

myErrCatch:
'log error
Resume Next

Create an Exist code from .VBS then exit Main.vbs

DIM returnValue
 returnValue = 99
 WScript.Quit(returnValue)
Community
  • 1
  • 1
Hiten004
  • 2,425
  • 1
  • 22
  • 34
  • VBScript not support `On Error Resume myErrCatch` kind of syntax. And could you please tell me why the `EXIT CODE` has been set as `99`? – CodeLover Dec 31 '12 at 17:40
  • VBScript does support error handling [MSDN Link with sample](http://msdn.microsoft.com/en-us/library/windows/desktop/ms675540(v=vs.85).aspx) – Hiten004 Dec 31 '12 at 19:02
  • you can put any number.. 99 was an sample. – Hiten004 Dec 31 '12 at 19:03
  • Yes I meant the way you wrote,its possible in VBA not in vbscript? I think so! `On Error Resume Next` and `On Error go to 0` is only supported by VBScript! Can you construct my code with your suggestion.I am bit confused about how would I write your one with my one! – CodeLover Dec 31 '12 at 19:10