2016-10-04 120 views
2

我試圖從HTA文件內啓動批處理文件。批處理文件的啓動似乎正常啓動(或至少是相關的CMD提示),但批處理會在稍後關閉,此時應耗時約5分鐘。在短暫的CMD過程正在運行的過程中,HTA窗口似乎暫停,然後在CMD過程結束後立即關閉。有關HTA的其他一切都可以正常運行。從HTA內啓動批處理文件

目標是讓HTA在後臺啓動批處理文件(隱藏),並且在處理批處理文件時,不會影響HTA。批處理文件完成並退出後,HTA將啓動一個包含用戶信息的新HTA。

這裏的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> 
+0

您還應該發佈'install.cmd'的源代碼 – Hackoo

回答

0

Windows不提供可變%curdir%的環境。它的擴展產生一個字符串%curdir%,所以命令提示符可能立即關閉,因爲找不到文件%curdir%_install.cmd。你的意思是%cd%?該變量僅在CMD中可用。

你真的想從第一個地方從當前工作目錄運行腳本嗎?我想說,你更有可能是從HTA的同一個目錄運行批處理文件。該位置可以這樣確定:

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

如果沒有幫助更改線路

Shell.Run Continue,0,True 

到這一點:

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

運行與cmd /k批處理腳本保持命令在(嘗試)腳本執行後提示打開,以便查看是否有任何錯誤。

+0

變量%curdir%預先填充了%〜dp0的值。當我msgbox該變量時,它顯示正確。至於批處理文件,它位於HTA文件所在的一個目錄中。至於你的建議,我對Shell.Run命令進行了更改,現在我可以看到CMD窗口打開,但它只是坐在提示符處,並沒有運行應該在HTA上播放的批處理文件和動畫只要CMD提示符保持打開狀態,屏幕就會暫停。 – user3208239

+0

你是否對包含'@echo hello'這一行的批處理文件有同樣的效果? –

+0

不是。一個簡單的批處理文件,只有echo hello,後面跟着一個暫停,顯示得很好,很花哨。我想知道這是否是因爲在原始批處理文件中調用了不同的批處理文件?我要進行一些更多的測試以找出答案。感謝名單。 – user3208239

0

因此,經過一些故障排除後,批處理腳本中的問題是配置錯誤的開始語句。

開始聲明是這樣的:

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

我改成了這樣:

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

我顯然需要對標題添加到啓動命令,否則啓動命令想到了第一次報文本是標題,然後開關應用於啓動,這是行不通的。