Forum Discussion

torus's avatar
torus
Contributor
7 months ago

How to run a batch (*.bat) file from TestComplete (Javascript)

It took me longer than I had hoped to figure this out so wanted to put the answer here in case this benefits anyone else. 

  • function runBatchFile()
    {
       WshShell.Run("C:\\Temp\\hello.bat")
    }

    Others posted some really good alternative answers as well ...

    • torus's avatar
      torus
      Contributor

      Thanks rraghvani for posting the documentation pertaining to this post. For someone like myself, it helped to have the exact code example due to not fully understanding the concepts behind the WshShell command.

  • MW_Didata's avatar
    MW_Didata
    Regular Contributor
    Function RunBatch(BatFile, WaitJN)
      'Run the command and wait for completion
      
      'Parameters : File, Size&state of window, Wait for completion
    
      RunBatch= Sys.OleObject("WScript.Shell").Run ((BatFile), 1, (WaitJN))
    
    End Function

    I use this for running Batchfiles, This way I can use the (WaitJN) parameter to either wait for the batch to be done or run it while TC does other things.

     

    EDIT: I posted the VBScript code, For JavaScript I use this one:

    function RunBatch(Batfile, WaitJN)
    {
      Log.Message("Running Batchfile '" + (Batfile) + "'", "");
      Sys.OleObject("WScript.Shell").Run ((Batfile), 1, (WaitJN))
    }

    The parameters work the same.

    • torus's avatar
      torus
      Contributor

      To anyone else who comes to this discussion, MW_Didata's example is VBScript. Thanks MW_Didata for the information and example code!

      • MW_Didata's avatar
        MW_Didata
        Regular Contributor

        oops you're right, I meant to post this one

        function RunBatch(Batfile, WaitJN)
        {
          Log.Message("Running Batchfile '" + (Batfile) + "'", "");
          Sys.OleObject("WScript.Shell").Run ((Batfile), 1, (WaitJN))
        }

         

  • nastester's avatar
    nastester
    Regular Contributor

     

    I use this:

    function runBatchFile(batchFilePath) {
      
        try {
         
          var process = Sys.OleObject("WScript.Shell");
          process.Run('cmd.exe /C "' + batchFilePath + '"', 1, true);
          
          while (process.Status == 0) {
            Delay(100);
          }
          
        } 
        
        catch (e) {
          Log.Error("An error occurred while running the batch file: " + e.message);
        }
         Log.Message("Batch file executed")
        
      }
    
    var batchFilePath = Project.ConfigPath + "SmokeTestCleanup.bat";

     

    • torus's avatar
      torus
      Contributor

      Thanks for the tip nastester. That worked great. I tried to get the cmd.exe /C <batchFile> to work for a while but was unsuccessful (I might not have been putting in the double back slashes and might have messed up where I placed the quotes). Your example works perfectly plus has error catching. Thanks for the tip!

  • function runBatchFile()
    {
       WshShell.Run("C:\\Temp\\hello.bat")
    }

    Others posted some really good alternative answers as well ...