【问题标题】:vbscript execute command to filevbscript 执行命令到文件
【发布时间】:2016-10-23 19:11:58
【问题描述】:

我正在尝试从 vbscript 中执行批处理命令。

我希望将输出写入文件。

当我运行它时,我没有得到控制台或文件的输出。

我正在尝试:

Dim objShell, command
Set objShell = WScript.CreateObject ("WScript.shell")
command = "wmic product get Name > textfile.txt"
objShell.Run command, 0, True 
Set objShell = Nothing

我做错了什么?

当我在命令提示符下运行wmic 命令时,它运行良好。

【问题讨论】:

    标签: batch-file vbscript wmic


    【解决方案1】:

    我需要在命令前面加上cmd比如:

    command = "cmd wmic product get Name > textfile.txt"
    

    除了使用 cmd 关键字之外,您还可以根据需要使用几个开关。你可以通过输入 cmd /? 来查看它们。在命令提示符窗口中。

    这里有一对:

    /C      Carries out the command specified by string and then terminates
    /K      Carries out the command specified by string but remains
    /S      Modifies the treatment of string after /C or /K (see below)
    /Q      Turns echo off
    /D      Disable execution of AutoRun commands from registry (see below)
    /A      Causes the output of internal commands to a pipe or file to be ANSI
    /U      Causes the output of internal commands to a pipe or file to be Unicode
    

    【讨论】:

    • 你应该使用cmd /c ... 而不仅仅是cmd ...
    • 我确实在我的实际脚本中使用了 /c ,但这并不是在所有情况下都正确。我想让答案保持一般性。
    【解决方案2】:
    command = "cmd wmic product get Name > textfile.txt"
    

    为此修改

    command = "cmd wmic product get Name >> textfile.txt"
    

    【讨论】:

      猜你喜欢
      • 2011-07-24
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多