Forum Discussion

cokyn's avatar
cokyn
Contributor
13 years ago

CHDIR and run *.bat file from the current dir

I would like to run bat file from script but when I tried to do that I encoutered error. That was because the bat file needs to be started from certain location (there are defined relative paths inside).

In my case, the location is "c:\Program Files (x86)\SmartAIM\ESI_Client\run.bat"  If I run bat from the location (c:\Program Files (x86)\SmartAIM\ESI_Client) everythig is working well, but so far I was not able to change dir and run the bat file from script. I tried something like this:




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


  ESI_run_bat_location="\"c:\\Program Files (x86)\\SmartAIM\\ESI_Client\"";


  Log.Message(ESI_run_bat_location);


  WScriptObj.Exec("chdir "+ESI_run_bat_location);


  WScriptObj.Exec(run.bat);



Any ideas please?



4 Replies

  • Thank you Tanya, but my point was I needed first somehow change directory, set is as local and then run the run.bat file from this location. I finally found the solution:




      var ESI_run_bat_location=(ini.GetSubSection("CAD_settings").GetOption("EsiRoot", "")); 


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


      Log.Message("Esi root location is set to the value of: "+ESI_run_bat_location);


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


     


      WScriptObj.Run("cmd /K CD "+ESI_run_bat_location+ "& run.bat", 1, false);



    where var ESI_run_bat_location=c:\Program Files (x86)\SmartAIM\ESI_Client


  • Hi Martin,


     


    I suppose you need to add "\\" between the folder and the file name in your path:




    WScriptObj.Exec("chdir "+"\\"+ESI_run_bat_location);




     


    Does this work?


     

  • Hi Tanya, I am sorry for my late response.

    Unfortunatelly it did not resolve my problem.




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


      ESI_run_bat_location="c:\\Program Files (x86)\\SmartAIM\\ESI_Client";


      Log.Message("chdir "+"\\"+ESI_run_bat_location);


      WScriptObj.Exec("chdir "+"\\"+ESI_run_bat_location);



    Your suggested command means that TestComplete would execute the following command - 

    chdir \c:\Program Files (x86)\SmartAIM\ESI_Client

    I am receiving the error "the system cannot find the file specified"

    and I am receiving the same error even if I run just WScriptObj.Exec("chdir");

    Any clue?



    Martin








  • Hi Martin,


     


    It looks like I gave you a wrong example. Sorry for this.


    Can you run your bat file by using this script?


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


    ESI_run_bat_location="\"c:\\Program Files (x86)\\SmartAIM\\ESI_Client\"";


    Log.Message(ESI_run_bat_location);


    WScriptObj.Exec(ESI_run_bat_location + "\\" + "run.bat");