HTA & Batch Hybrid, passing variables from BATCH section

前端 未结 1 1975
刺人心
刺人心 2021-01-13 19:06

I\'m attempting to write a batch + hta hybrid script that will allow me to pass variables from the batch section of the script to the hta section, so that way I can generate

相关标签:
1条回答
  • 2021-01-13 19:58

    You can access environment variables in the HTA runtime by using the Wscript.Shell COM object's Environment object. You can pass data back from HTA to the Batch thread over stdout by using Scripting.FileSystemObject's GetStandardStream method. Here's a demonstration of both:

    <!-- :: Batch section
    @echo off & setlocal
    Pushd "%~dp0"
    
    FOR /F "tokens=2 delims==" %%A in ('wmic ComputerSystem Get Model /value') do SET model=%%A
    
    for /F "delims=" %%a in ('mshta.exe "%~f0"') do set "HTAreply=%%a"
    echo End of HTA window, reply: "%HTAreply%"
    goto :EOF
    -->
    <script language="VBScript">
    
        Set Env = CreateObject("Wscript.Shell").Environment("Process")
        Set StdOut = CreateObject("Scripting.FileSystemObject").GetStandardStream(1)
    
        MsgBox Env("model")
        StdOut.Write("response")
    
        Set Env = Nothing
        Set StdOut = Nothing
    
        close()
    
    </script>
    

    For what it's worth, you can also access VBScript in a hybrid format using cscript by kludging a pretend .wsf file extension. The advantage, besides getting rid of the brief flicker of an HTA window appearing and disappearing, is that you can pass script arguments directly without having to access the Environment("Process") scope.

    <!-- : batch portion
    @echo off & setlocal
    
    FOR /F "tokens=2 delims==" %%A in ('wmic ComputerSystem Get Model /value') do SET model=%%A
    
    for /F "delims=" %%a in ('cscript /nologo "%~f0?.wsf" "%model%"') do set "VBreply=%%a"
    echo End of VB script, reply: "%VBreply%"
    
    goto :EOF
    
    : VBScript -->
    <job>
        <script language="VBScript">
            model = WScript.Arguments(0)
    
            MsgBox model
            Wscript.Echo "response"
        </script>
    </job>
    

    And hybrid Batch + JScript is even easier. It's also possible to have both VBScript and JScript code as multiple jobs with the .wsf method.

    0 讨论(0)
提交回复
热议问题