Forum Discussion

wynfranc1234's avatar
wynfranc1234
Contributor
10 years ago
Solved

Different Exit code showing when there is no Delay

Hi.



I executed a program from a .BAT file and the error code it returns is 100. I created a JScript that executes that .bat file that runs a wscript shell . When I displayed the ExitCode with no Delay command before the Log message , the Exit code shows up as zero, but if I put a delay(100) the proper exit code appears. Why is that?  




WshShell = new ActiveXObject("WScript.Shell");



 



 



 



oExec = WshShell.Exec(CommandLine);



Log.Message("Exit Code is " + oExec.ExitCode)



 



 



 



  • Hi Dell,



    Exec doesn't wait for the command to complete, that's why you need delays. You need to wait until oExec.Status becomes 1:



    var WshShell = new ActiveXObject("WScript.Shell");

    var oExec = WshShell.Exec(CommandLine);



    while (oExec.Status == 0) {

      Delay(100);

    }



    Log.Message("Exit Code is " + oExec.ExitCode);



    But if you don't need to work with the input/output streams of your batch file, you can run it using WshShell.Run instead, which may be easier:



    var WshShell = new ActiveXObject("WScript.Shell");

    var res = WshShell.Run(CommandLine, 0, true /* wait to return */);

    Log.Message("Exit Code is " + res);


2 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)


    Hi Dell,



    Exec doesn't wait for the command to complete, that's why you need delays. You need to wait until oExec.Status becomes 1:



    var WshShell = new ActiveXObject("WScript.Shell");

    var oExec = WshShell.Exec(CommandLine);



    while (oExec.Status == 0) {

      Delay(100);

    }



    Log.Message("Exit Code is " + oExec.ExitCode);



    But if you don't need to work with the input/output streams of your batch file, you can run it using WshShell.Run instead, which may be easier:



    var WshShell = new ActiveXObject("WScript.Shell");

    var res = WshShell.Run(CommandLine, 0, true /* wait to return */);

    Log.Message("Exit Code is " + res);