2017-06-10 86 views
1

标准输出当SHCMD写剧本,你可以在一行的末尾把>有该行重定向到一个文件的输出。如果不是,则发送到标准输出。重定向到VBS

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

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

+0

我已经通过已经搜索过的解决方案进行了搜索,他们没有为我工作(或者我没有能够让他们工作)。 – Envite

回答

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

相关链接:


+0

谢谢。我不需要使用cscript,因为我需要从VBS启动Excel,但答案正是我所要求的。 – Envite