2017-07-02 76 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()