How to launch a batch file from within an HTA

20,735

You need to use /C or /K parameter as first argument when launching CMD process anywhere.

 /C     Run Command and then terminate

 /K     Run Command and then return to the CMD prompt.

Try:

WshShell.Run "cmd.exe '/C example.bat'"

But that is only the explanation about why your script does not work because don't need to use CMD, you can run directly the batch file 'cause ShellExecute.

Share:
20,735

Related videos on Youtube

Bajan
Author by

Bajan

Updated on April 25, 2020

Comments

  • Bajan
    Bajan about 4 years

    I have a basic HTA in which I am trying to launch a batch file via a button, however when I try to launch the batch file, it does not run. When I press the button, a command prompt window launches but does not run the script.

    Here is as simple example of the code:

    <HTML>
      <HEAD>
        <TITLE>Fix</TITLE>
      </HEAD>
      <BODY>
        <FORM>
          <INPUT TYPE="Run" NAME="Button" VALUE="Click">
    
          <SCRIPT FOR="Button" EVENT="onClick" LANGUAGE="VBScript">
            Set WshShell = CreateObject("WScript.Shell")
            WshShell.Run "cmd.exe '.\example.bat'"
          </SCRIPT>
    
        </FORM>
      </BODY>
    </HTML>
    

    How can I launch a batch file from the same working directory (or sub-directory) as my HTA?

    Thank you

    • Matt Williamson
      Matt Williamson about 11 years
      Have you tried just calling the batch file by itself instead of calling cmd.exe first?
    • Bajan
      Bajan about 11 years
      Thanks Matt, this worked for me as well as the other suggestion.
  • Bajan
    Bajan about 11 years
    Hi, I tried it with the /C switch but it just launched a black cmd box briefly and closes, but doesn't execute the script. I was able to get it working with Matt's suggestion to just call the .bat file directly, as in: WshShell.Run "example.bat" Thanks for your help!
  • Bajan
    Bajan about 11 years
    UPDATE: My Apologies, that does indeed work as well! I was trying to run it via a UNC path before. Thanks for this!
  • ElektroStudios
    ElektroStudios about 11 years
    np Bajan but if my answer resolved your problem then please use the Accept button to accept my answer. regards.
  • Andrestand
    Andrestand over 3 years
    In my case this doesn't work unless I leave an space between .bat and the '".