2017-07-02 73 views
0

我有一個批處理文件,它從命令行獲取一些參數並將一些值返回到STDOUT。VBScript腳本作爲無聲批處理文件的包裝

我希望批處理文件是「無聲的」(例如控制檯沒有顯示),並且發現使用vbs腳本的唯一可能性。

我用this thread來實現參數轉發到VBS中的批處理文件。

然後,我用下面的命令來調用批處理文件,我包:

CreateObject("WScript.Shell").Run batchFilePath & " " & Trim(arglist), 0, False 

事實證明,我的批處理文件並運行,但它的標準輸出的地方丟棄,而不會使其歸途被稱爲VBS劇本的人。即批處理文件的STDOUT不會重定向到VBS腳本的STDOUT中。

如何將批處理文件的STDOUT重定向到VBS腳本STDOUT,如果我從某個shell啓動VBS腳本,批處理文件的輸出也會打印到shell?

回答

0

嘗試......

Intreturn = WshShell.Run("cmd /c " & path& " " & args & ">c:\batchoutput.txt", 0, true) 
Set fso = CreateObject("Scripting.FileSystemObject") 
Set objfile = fso.OpenTextFile("c:\batchoutput.txt", 1) 
text = objfile.ReadAll 
Objfile.Close 

或者試試這個...

Set WshShell = WScript.CreateObject("WScript.Shell") 

Set objexc = WshShell.Exec("cmd /c " & command and args) 'replace command and args with proper variables 
strOutputText = "" 
While Not objexc.StdOut.AtEndOfStream 
    strOutputText = strOutputText & objexc.StdOut.ReadLine() 
Loop 

Msgbox strOutputText 

您可能需要一些這方面的調試。

+0

一個子進程的輸出重定向到你自己的標準輸出的唯一方法是通過重定向到文件中,然後讀取它,並打印出來? – SomethingSomething

+0

添加了進一步的代碼 –

1

使用Exec的,而不是運行,像這樣:

set objShell = CreateObject("WScript.Shell") 
cmd = "echo Hello World!" 
' Run the process 
set objRes = objShell.Exec("cmd /c """ & cmd & """") 
' Wait for the child process to finish 
Do While objRes.Status = 0 
    WScript.Sleep 100 
Loop 
' Show whatever it printed to its standard output 
Wscript.Echo "The output was:" & vbNewLine & objRes.StdOut.ReadAll()