How to return exit status from an HTA (vbscript) to the calling Batch file ?
My batch file code
@echo OFF
echo Configuring Test...
call AppConfigurationEditor.hta
call TestConfigurationEditor.hta
1.If user click on CANCEL button on the first HTA I dont want to run 2nd HTA.
2.Batch script calls/displays the 2nd HTA immediately, not wait till closing of 1st HTA.
HTAs have no implemented way of returning an errorlevel to the caller process.
WScript.Quit
can not be used. The lenguage engine vbscript/javascript/... is the same, but the host object that instantiates the engine is not the habitual windows scripting host, it is a browser object, soWScript
object does not exist.window.close
method can not be used. It can close the hta, but will give no return value.The usual way of using an hta to return a value to the calling process is to persist this value inside a file or in the registry. Then the calling process can retrieve the required values.
If errorlevel is needed, there is no direct method. But an indirect method can be implemented. Just use WMI to retrieve the list of running processes, locate the current hta, and for this process call the Terminate method which allow to set an exit value.
Then you can call it from batch file as
You have to return the result of the pressed button as
Exit Code
that can be recuperated in theerrorlevel
, withWscript.Quit [Value]
in your two Hta code (or in the first if you just want to check the first)Exemple :
Test.vbs
runtest.bat
should get the job done - provided
errorlevel
is returned as 0 forterminated normally
and something else for 'cancelled'.The
echo %errorlevel%
line is simply there for testing, just showing you what's going on.start /wait
will start the target, and pause the batch until the process finishes. The extra pair of rabbit's ears actually enclose the new window title - redundant here, but best to retain asstart
otherwise picks the first quoted string from the target (if any) as a window title.