重定向到VBS中的标准输出

时间:2017-06-10 15:58:08

标签: vbscript

sh cmd 中编写脚本时,您可以在行尾添加> 以获得输出该行重定向到文件。如果没有,则将其发送到标准输出。

此外,两者都有 echo 命令来产生标准输出的输出(反过来也可以重定向)。

如何在VBS脚本中执行这两件事?

1 个答案:

答案 0 :(得分:1)

没什么不同。您只需确保使用基于控制台的脚本宿主 cscript 运行脚本。

myscript.vbs:

' WScript.Echo is a host-aware hybrid method. 
' in cscript, prints the message with final a new line feed, also accepts vary arguments (0 or more).
' in wscript, shows a message dialog window
WScript.Echo "test1", "arg2"

' WScript.Stdout.Write on the other hand is a more traditional way to write to standard output stream
' print the message as is with no new line feeds
WScript.Stdout.Write "test"
WScript.Stdout.Write "2"

命令:

cscript myscript.vbs

输出:

Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

test1 arg2
test2

还有一个选项可以阻止在输出上显示横幅。

cscript //NoLogo myscript.vbs

输出:

test1 arg2
test2

重定向:

cscript //NoLogo myscript.vbs>output.txt

PS:cscript是仅在Windows Server操作系统上的默认脚本解释器。否则默认为wscript。因此,最好使用特定的脚本宿主运行脚本。

要更改默认脚本主机,请查看Running Your Scripts

有用的链接: