问题
I have created HTA file which has userform to collect data from the user. I am calling this HTA file from batch file.
After reading user inputs, i want input values to be passed to batch file from HTA. Is is possible to achieve?
回答1:
Yes, you can have the HTA file return the values to the batch file, but it cannot do it directly. You have to use Javascript to create a text file with the user provided values, then your batch file can process the values. Here's a site with several methods listed to read and write text files:
http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm
Using one of these, I created a very simple example to demonstrate how this is done:
SimpleForm.hta
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
function WriteFile() {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.CreateTextFile("c:\\Output.txt", true);
fh.WriteLine(myForm.FN.value + '~' + myForm.LN.value);
fh.Close();
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="myForm">
<P>First Name: <INPUT name="FN" type="text"></P>
<P>Last Name: <INPUT name="LN" type="text"></P>
<P><INPUT type="button" value="Save Values" onclick="WriteFile();"></P>
</FORM>
<P>After you click 'save', close the window.</P>
</BODY>
</HTML>
Now the batch file:
@echo off
start /wait SimpleForm.hta
for /f "tokens=1,2 delims=~" %%i in (C:\Output.txt) do (
set FirstName=%%i
set LastName=%%j
)
del C:\Output.txt
echo The user entered %FirstName% %LastName% for their name.
You will need to deal with the complexities of the user typing in the delimiter character (a tilde in my example) into one of the text fields and throwing off parameter parsing of the for
statement. You could use an obscure character that the user won't type for instance.
来源:https://stackoverflow.com/questions/14826682/using-hta-with-batch-file