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.
Related videos on Youtube
Comments
-
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 about 11 yearsHave you tried just calling the batch file by itself instead of calling cmd.exe first?
-
Bajan about 11 yearsThanks Matt, this worked for me as well as the other suggestion.
-
-
Bajan about 11 yearsHi, 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 about 11 yearsUPDATE: My Apologies, that does indeed work as well! I was trying to run it via a UNC path before. Thanks for this!
-
ElektroStudios about 11 yearsnp Bajan but if my answer resolved your problem then please use the Accept button to accept my answer. regards.
-
Andrestand over 3 yearsIn my case this doesn't work unless I leave an space between
.bat
and the'"
.