How to launch a batch file from within an HTA

Bajan picture Bajan · Apr 24, 2013 · Viewed 19.4k times · Source

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

Answer

ElektroStudios picture ElektroStudios · Apr 24, 2013

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.