将HTA与批处理文件一起使用 [英] using HTA with batch file

查看:113
本文介绍了将HTA与批处理文件一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了HTA文件,该文件具有用户窗体,可以从用户那里收集数据. 我正在从批处理文件中调用此HTA文件.

I have created HTA file which has userform to collect data from the user. I am calling this HTA file from batch file.

阅读用户输入后,我希望将输入值从HTA传递到批处理文件. 有可能实现吗?

After reading user inputs, i want input values to be passed to batch file from HTA. Is is possible to achieve?

推荐答案

是的,您可以让HTA文件将值返回到批处理文件,但不能直接执行.您必须使用Javascript创建带有用户提供的值的文本文件,然后您的批处理文件才能处理这些值.这是一个列出了几种读取和写入文本文件的方法的站点:

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

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>

现在批处理文件:

@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.

您将需要处理以下复杂性:用户在文本字段之一中输入定界符(在我的示例中为波浪号),并抛弃for语句的参数解析.您可以使用用户不会键入的晦涩字符,例如.

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.

这篇关于将HTA与批处理文件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆