运行命令并捕获vbscript中的输出

时间:2015-01-22 11:58:51

标签: vbscript

我正在尝试运行以下命令并使用VBscript返回它的输出:

dir /A-d "C:\Windows\Minidump" | find /c "/"

我有以下脚本,但它不起作用(可能是因为" charachters:

Wscript.Echo runCMD("dir /A-d "C:\Windows\Minidump" | find /c "/"")

Function runCMD(strRunCmd)

 Set objShell = WScript.CreateObject("WScript.Shell")
 Set objExec = objShell.Exec(strRunCmd)

 strOut = ""

 Do While Not objExec.StdOut.AtEndOfStream
  strOut = strOut & objExec.StdOut.ReadLine()
 Loop

 Set objShell = Nothing
 Set objExec = Nothing

 runCMD = strOut

End Function

有关如何实现这一目标的任何建议吗?

1 个答案:

答案 0 :(得分:3)

  1. dir是固有的;你需要%comspec%
  2. 双引号需要在VBScript中用双引号转义:

    Wscript.Echo runCMD("%comspec% /c dir /A-d ""C:\Windows\Minidump"" | find /c ""/""")