我需要在Visual Basic脚本执行命令行(I need execute a command l

2019-06-26 07:50发布

我需要执行我的VBS命令“版本”,看看我的操作系统的版本,我不知道如何做到这一点。

我想这一点,但不工作:

Function ExecuteWithTerminalOutput(cmd)
Set shell = WScript.CreateObject("WScript.Shell")
Set Exec =  shell.Exec("ver")
End Function

Answer 1:

尝试是这样的:

Dim objShell
Set objShell = WScript.CreateObject ("WScript.shell")
objShell.run "cmd /c ver"
Set objShell = Nothing

编辑:

那么,你可以输出重定向到一个文件,然后读取文件:

return = WshShell.Run("cmd /c ver > c:\temp\output.txt", 0, true)

Set fso  = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("c:\temp\output.txt", 1)
text = file.ReadAll
file.Close


Answer 2:

有一种方法可以做到这一点,而无需编写输出到文件。

例如,假设你想捕捉到一个目录列表的文本。 (会有很多更好的方法来获得比这一点,但我只是用一个简单的例子。)

一个具有如下功能在VBScript中,您可以输入:

thisDir = getCommandOutput("cmd /c dir c:")

并执行上面的行的情况下,变量“THISDIR”将包含从DIR命令的输出。

请注意,你想从输出某些命令将要求您通过命令外壳(以下简称“CMD / C”以上的部分),通过他们,而如果你不直接shell中运行他们其他人可能会正常工作。 尝试没有命令shell。 如果失败,用命令shell尝试。

'
' Capture the results of a command line execution and
' return them to the caller.
'
Function getCommandOutput(theCommand)

    Dim objShell, objCmdExec
    Set objShell = CreateObject("WScript.Shell")
    Set objCmdExec = objshell.exec(thecommand)
    getCommandOutput = objCmdExec.StdOut.ReadAll

end Function


Answer 3:

Dim shell
Set shell= WScript.CreateObject ("WScript.shell")
shell.Exec"cmd /c ver"
Set shell= Nothing


文章来源: I need execute a command line in a Visual Basic Script