user3208239
user3208239

Reputation: 771

Launching batch file from within HTA

I'm trying to launch a batch file from within a HTA file. 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. During the brief moment the CMD process is running, HTA window appears to pause, then closes as soon as the CMD process ends. Everything else about the HTA functions properly.

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. Once the batch file has completed and exited, the HTA will launch a new HTA with information for the user.

Here's the HTA I have that is not functioning properly...

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

Upvotes: 2

Views: 1917

Answers (2)

user3208239
user3208239

Reputation: 771

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.

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200373

Windows doesn't provide an environment variable %curdir%. Its expansion produces a literal string %curdir%, so the Command Prompt probably closes immediatly because a file %curdir%_install.cmd cannot be found. Did you perhaps mean %cd%? That variable is only available within 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. 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.

Upvotes: 0

Related Questions