带 HTA 的批处理输入表单

Input form for batch with HTA

如何在 HTA 中制作一个简单的输入表单并在一个简单的变量中批量读取它, 和一个继续的按钮。我不知道还有什么其他方法可以为批处理文件制作像样的 GUI。 像这样

感谢任何帮助。

总之,您只需

  • 获取对.hta文件内标准输出流的引用,并将字段内容写入此输出

  • 使用 for /f 命令从批处理文件执行 .hta 以处理发送到标准输出的数据。

唯一的 "problem" 是您需要使用 .hta 文件的完整路径显式调用 mshta.exe 可执行文件。

serialNumber.hta

<HTML>
    <HEAD>
        <HTA:APPLICATION 
            ID              = "serialHTA" 
            APPLICATIONNAME = "serialHTA"
            VERSION         = "0.1"
            NAVIGABLE       = "yes"
            SHOWINTASKBAR   = "yes" 
            SINGLEINSTANCE  = "yes" 
            WINDOWSTATE     = "normal"

            BORDER          = "normal" 
            BORDERSTYLE     = "normal"
            INNERBORDER     = "no" 

            CAPTION         = "yes" 
            MINIMIZEBUTTON  = "yes"
            MAXIMIZEBUTTON  = "yes"
            SYSMENU         = "yes" 
            SCROLL          = "yes" 
            SCROLLFLAT      = "yes"

            CONTEXTMENU     = "yes"
            SELECTION       = "yes"
        />

        <TITLE>input serial number</TITLE>

        <SCRIPT language="Javascript">
            function closeHTA(sendOutput){
                if (sendOutput){
                    (new ActiveXObject('Scripting.FileSystemObject'))
                        .GetStandardStream(1)
                        .WriteLine(
                            document.getElementById('serialNumber').value
                        );
                };
                window.close();
            }
        </SCRIPT>

    </HEAD>

    <BODY>
        <label for="serialNumber">serial:</label>
        <input type="text" id="serialNumber">
        <br>
        <button onclick="closeHTA(true);">ok</button>
        <button onclick="closeHTA();">cancel</button>
    </BODY>

</HTML>

serialNumber.cmd

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "serialNumber="
    for /f "delims=" %%a in ('mshta.exe "%~dp0\serialnumber.hta"') do set "serialNumber=%%a"

    if defined serialNumber (
        echo Serial number: %serialNumber%
    ) else (
        echo No serial number provided
    )

或者您可以 运行 从一个文件中获取它

<!-- :: Batch section
@echo off
    setlocal enableextensions disabledelayedexpansion

    set "serialNumber="
    for /f "delims=" %%a in ('mshta.exe "%~F0"') do set "serialNumber=%%a"

    if defined serialNumber echo %serialNumber%
pause
-->
<HTML>
    <HEAD>
        <HTA:APPLICATION 
            ID              = "serialHTA" 
            APPLICATIONNAME = "serialHTA"
            VERSION         = "0.1"
            NAVIGABLE       = "yes"
            SHOWINTASKBAR   = "yes" 
            SINGLEINSTANCE  = "yes" 
            WINDOWSTATE     = "normal"

            BORDER          = "normal" 
            BORDERSTYLE     = "normal"
            INNERBORDER     = "no" 

            CAPTION         = "yes" 
            MINIMIZEBUTTON  = "yes"
            MAXIMIZEBUTTON  = "yes"
            SYSMENU         = "yes" 
            SCROLL          = "yes" 
            SCROLLFLAT      = "yes"

            CONTEXTMENU     = "yes"
            SELECTION       = "yes"
        />

        <TITLE>input serial number</TITLE>

        <SCRIPT language="Javascript">
            function closeHTA(sendOutput){
                if (sendOutput){
                    (new ActiveXObject('Scripting.FileSystemObject'))
                        .GetStandardStream(1)
                        .WriteLine(
                            document.getElementById('serialNumber').value
                        );
                };
                window.close();
            }
        </SCRIPT>

    </HEAD>

    <BODY>
        <label for="serialNumber">serial:</label>
        <input type="text" id="serialNumber">
        <br>
        <button onclick="closeHTA(true);">ok</button>
        <button onclick="closeHTA();">cancel</button>
    </BODY>

</HTML>