简体   繁体   English

从HTA内部启动批处理文件

[英]Launching batch file from within HTA

I'm trying to launch a batch file from within a HTA file. 我正在尝试从HTA文件中启动批处理文件。 The launching of the batch file appears to start properly (or at least the associated CMD prompt), but the batch closes moments later, when it should take approximately 5 minutes. 批处理文件的启动似乎正常开始(或至少关联了CMD提示),但是批处理在稍后关闭,大约需要5分钟。 During the brief moment the CMD process is running, HTA window appears to pause, then closes as soon as the CMD process ends. 在CMD进程运行的短暂时间内,HTA窗口似乎暂停,然后在CMD进程结束时关闭。 Everything else about the HTA functions properly. 关于HTA的其他所有功能均正常运行。

The goal is to have the HTA launch the batch file in the background (hidden) and while the batch file is processing, have no affect on the HTA. 目标是让HTA在后台启动(隐藏)批处理文件,并且在处理批处理文件时,对HTA没有影响。 Once the batch file has completed and exited, the HTA will launch a new HTA with information for the user. 批处理文件完成并退出后,HTA将启动包含用户信息的新HTA。

Here's the HTA I have that is not functioning properly... 这是我拥有的HTA,无法正常运行...

<html>
  <head>
    <style>
      body { background:#fff url('../_dependencies/welcome.jpg') no-repeat center center fixed; color:#000; margin:25px; padding:0; }
      div#gap { height:306px; }
      div#buttons { padding-right:12px; position:absolute; right:0; }
    </style>
    <title>Installer</title>
    <script language="vbscript">
      Sub Window_OnLoad
        Set Shell = CreateObject("WScript.Shell")
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        sPath = Shell.ExpandEnvironmentStrings("%curdir%")
        Continue = Chr(34) & sPath & "_install.cmd" & Chr(34)
        Shell.Run Continue,0,True
        CompName = Shell.ExpandEnvironmentStrings("%computername%")
        Const ForAppending = 8
        textFile = sPath & "_Logs\" & CompName & ".upgraded.txt"
        If Not objFSO.FileExists(textFile) Then
          Set objTextFile = objFSO.CreateTextFile(textFile, True)
          objTextFile.Close
        End If
        Set objTextFile = objFSO.opentextfile(textFile,ForAppending)
        objTextFile.WriteLine("Upgrade complete on this computer." & vbCrLf & Now())
        objTextFile.Close
        Set textFile = Nothing
        self.close()
      End Sub
    </script>
    <script language="javascript">
      window.resizeTo(620,365);
      window.moveTo((screen.width-620)/2,(screen.height-365)/2);
    </script>
    <hta:application applicationname="Installer" border="none" caption="no" id="objnotitlebar" innerborder="no" maximizebutton="no" minimizebutton="no" scroll="no" showintaskbar="no" singleinstance="yes" systemmenu="no">
  </head>
  <body>
    <div id="gap"><img src="../_dependencies/waiting.gif" /></div>
    <div id="buttons"></div>
  </body>
</html>

Windows doesn't provide an environment variable %curdir% . Windows没有提供环境变量%curdir% Its expansion produces a literal string %curdir% , so the Command Prompt probably closes immediatly because a file %curdir%_install.cmd cannot be found. 它的扩展会产生文字字符串%curdir% ,因此命令提示符可能会立即关闭,因为%curdir%_install.cmd文件%curdir%_install.cmd Did you perhaps mean %cd% ? 您可能是说%cd%吗? That variable is only available within CMD. 该变量仅在CMD中可用。

And do you actually want to run the script from the current working directory in the first place? 您是否真的想首先从当前工作目录中运行脚本? I'd say it's more likely that you mean to run the batch file from the same directory as the HTA. 我想说的是您更有可能要从与HTA相同的目录中运行批处理文件。 That location can be determined like this: 可以这样确定该位置:

dir = objFSO.GetParentFolderName(Replace(document.location.href, "file:///", ""))
sPath = objFSO.GetFolder(dir).Path

If that doesn't help change the line 如果那没有帮助改变线

Shell.Run Continue,0,True

into this: 到这个:

Shell.Run "%COMSPEC% /k " & Continue, 1, True

Running a batch script with cmd /k keeps the Command Prompt open after the (attempted) script execution, so you can see if there were any errors. 使用cmd /k运行批处理脚本后,(尝试执行的)脚本执行后,命令提示符将保持打开状态,因此您可以查看是否存在任何错误。

So, after a bit of troubleshooting, the issue in the batch script was a misconfigured start statement. 因此,在进行了一些故障排除后,批处理脚本中的问题是配置错误的启动语句。

The start statement was this: 开始语句是这样的:

@start /b /min /wait "upgradeinstaller.exe" /silent /noreboot

I changed it to this: 我将其更改为:

@start /b /min /wait "Upgrade Installer" "upgradeinstaller.exe" /silent /noreboot

I apparently needed to add the title to the start command, otherwise the start command thought the first quoted text was the title, then the switches were applied to start, which doesn't work. 我显然需要在启动命令中添加标题,否则启动命令会认为第一个引用的文本是标题,然后将开关应用于启动,这是行不通的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM