2013-08-02 45 views
1

這裏是我的VBS代碼等到特定的消息顯示在控制檯

Set wshshell = wscript.CreateObject("WScript.Shell") 
Wshshell.run "C:\Temp\Executable.exe -c -dir C:\Productdir" 
'Wait till "This will install the product on your computer. Press OK, Cancel" appears 
WScript.Sleep 10000 
WshShell.SendKeys "~" 
  1. 是否有可能「而不是10秒的硬編碼睡眠」添加像這樣的例如if consolemessage="This will install the product on your computer. Press OK, Cancel" then WshShell.SendKeys "~"
  2. 可以使用WScript.StdOut捕獲上述情況下的控制檯消息嗎?我無法做到。

回答

1

當您使用Exec方法執行程序時,您可以閱讀StdOut。再次

Set wshshell = wscript.CreateObject("WScript.Shell") 
Set p = Wshshell.Exec("C:\Temp\Executable.exe -c -dir C:\Productdir") 

Do While p.Status = 0 
    output = "" 
    Do Until p.StdOut.AtEndOfStream 
    c = p.StdOut.Read(1) 
    WScript.StdOut.Write c 'write read characters to the command prompt 
    output = output & c 
    If InStr(output, "This will install the product") > 0 Then 
     'do stuff 
     Exit Do 
    End If 
    Loop 
    WScript.Sleep 100 
Loop 
+0

感謝。我使用了ur邏輯,但是現在安裝沒有開始,遊標等待着。只要我現在運行此腳本,就會顯示此消息「Microsoft(R)Windows Script Host Version 5.8 Copyright(C)Microsoft Corporation。All rights reserved。」然後它無盡的等待。 – Kriti

+0

問題在於,在繼續執行'Do'循環之前,腳本正在等待'Set p = Wshshell.Exec(「...」)'行結束。 – aphoria

+0

@aphoria錯誤。 'Exec'異步運行該命令。 –