using HTA with batch file

≡放荡痞女 提交于 2019-12-11 13:42:51

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!