Forum Discussion

ssa's avatar
ssa
Occasional Contributor
10 years ago
Solved

Unable to execute a batch file from TC using JS.


I am trying to execute a .bat file that opens a FTP connection to a server and put a file on specified location.



If i manually issue this command in the command prompt



C:\>ftp -i -s:ftpConnection.bat



It send out the mentioned file to the specified path.



But i am unable to execute this script(ftpConnection.bat) from TC.



I tied to it like below..



Sys.OleObject("WScript.Shell").Run("C:\\Windows\\System32\\ftp.exe -i -s:C:\\ftpConnection.bat");



It just opens a command prompt and the file is not sent to the destination. Event the command prompt exits immediately so unable to see the message.



Searched a bit and found below link but couldn't make it to server my purpose.



Calling a DOS command



any idea how to do this form TC.



Thanks.



  • file is not sent to the destination
    Does your ftpConnection.bat file has the lcd command to specify the local folder that contains the files for upload? Like this:



    lcd C:\MyFolder

    mput *.txt


    Without lcd, ftp looks for files in the current working folder, which in this case is your TestComplete project folder.



    the command prompt exits immediately so unable to see the message.
    To leave the command prompt window open after running the command, use .Run("cmd /k command"):



    Sys.OleObject("WScript.Shell").Run("cmd /k ftp -i -s:C:\\ftpConnection.bat", 1, false);


    Or if you want to pause the script until the FTP transfer completes, use .Run(command, 1, true):



    Sys.OleObject("WScript.Shell").Run("ftp -i -s:C:\\ftpConnection.bat", 1, true);


6 Replies

  • jorgesimoes1983's avatar
    jorgesimoes1983
    Regular Contributor
    You can do something like this:



    function runShellExecute(pathBatFile) {

        Log.LockEvents(0);

        var pathProject;

        pathProject = Project.Path;

        pathProject += pathBatFile;

        var objShell = new ActiveXObject("shell.application");

        objShell.ShellExecute(pathBatFile, "", "pathProject", "open", 1);

        Delay(500);

        objShell = null;

        Log.UnlockEvents();

    }
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)


    file is not sent to the destination
    Does your ftpConnection.bat file has the lcd command to specify the local folder that contains the files for upload? Like this:



    lcd C:\MyFolder

    mput *.txt


    Without lcd, ftp looks for files in the current working folder, which in this case is your TestComplete project folder.



    the command prompt exits immediately so unable to see the message.
    To leave the command prompt window open after running the command, use .Run("cmd /k command"):



    Sys.OleObject("WScript.Shell").Run("cmd /k ftp -i -s:C:\\ftpConnection.bat", 1, false);


    Or if you want to pause the script until the FTP transfer completes, use .Run(command, 1, true):



    Sys.OleObject("WScript.Shell").Run("ftp -i -s:C:\\ftpConnection.bat", 1, true);


  • ssa's avatar
    ssa
    Occasional Contributor


    Tried it but same result. I think i am not clear about some thing like..




    • pathBatFile in my case it's "C:\\ftpConnection.bat"


    • What does  pathProject mean ?


    Does it mean the FTP path  "C:\\Windows\\System32\\ftp.exe" ?




     


  • jorgesimoes1983's avatar
    jorgesimoes1983
    Regular Contributor
    Project.Path gets the path tou you project, so the bat file must be in the root of that folder.



    If you want another path, you must adjust your function, and replace Project.Path variables, for your path.
  • jorgesimoes1983's avatar
    jorgesimoes1983
    Regular Contributor
    Like this should work:



    function runscript(){

      runShellExecute2("ipsequipa_mir.bat");

    }



    function runShellExecute2(pathBatFile) {

        var pathProject;

        pathProject += pathBatFile;

        var objShell = new ActiveXObject("shell.application");

        objShell.ShellExecute(pathBatFile, "", "c:\\", "open", 1);

        Delay(500);

        objShell = null;

    }