Forum Discussion

jsmunroe's avatar
jsmunroe
New Contributor
13 years ago

Running Windows Batch Files

Is there a way within the TestComplete API to run a Windows batch file at a given path? 
  • Hi Jordan,


    There is a couple of ways to do that:

    1. You can add your batch file as a TestedApps item.

    2. Run you batch file from the script:

    Sub Test

     Set obj = Sys.OleObject("WScript.Shell")

     obj.Run("c:\test.bat")

    End Sub

  • jsmunroe's avatar
    jsmunroe
    New Contributor
    The solution was to enclose my path in quotes so I had to do something like this. This is javascript, I apologize.



    var obj = Sys.OleObject("WScript.Shell");

      obj.Run("\"" + strMyPath +  "\"");



    It works wonderfully.
    • martin_facchini's avatar
      martin_facchini
      Contributor

      i had the same problem using the example from Allan (The system cannot find the file specified), and tried the suggested change from jsmunroe, which worked, but I’m relatively new to coding and don't understand why my original path in mypath didn't work, and why the one from jsmunroe did.

       

      Therefore, why does inserting additional "\"'s on either side of the path correct the problem?

       

       

          var obj = Sys.OleObject("WScript.Shell");
          var mypath = "C:\\PixelPOS\\TestComplete\\Default INI and Policies\\XMLPolicies Reset.bat"
          
          obj.Run("\"" + mypath +  "\"");

      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        This is because the path to your .bat file contains spaces ("C:\\PixelPOS\\TestComplete\\Default INI and Policies\\XMLPolicies Reset.bat") and without quotes the system looks for the "C:\\PixelPOS\\TestComplete\\Default INI" file that, obviously, does not exist.

        To get a visual illustration of the said above:) try to type

        C:\\PixelPOS\\TestComplete\\Default INI and Policies\\XMLPolicies Reset.bat

        in the command prompt, press Enter and check the result. Then type

        "C:\\PixelPOS\\TestComplete\\Default INI and Policies\\XMLPolicies Reset.bat"

        press Enter and check the result one more time.

  • jsmunroe's avatar
    jsmunroe
    New Contributor
    Thanks for your response. The second open worked best as this is a batch file that may or may not exist in a folder.  Though the FSO says the batch file exists I get the following exception:



    The system cannot find the file specified.