windows cscript - 在控制台的同一行上打印输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2897698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cscript - print output on same line on console?
提问by Guy
If I have a cscript that outputs lines to the screen, how do I avoid the "line feed" after each print?
如果我有一个将行输出到屏幕的 cscript,如何避免每次打印后出现“换行”?
Example:
例子:
for a = 1 to 10
WScript.Print "."
REM (do something)
next
The expected output should be:
预期的输出应该是:
..........
Not:
不是:
.
.
.
.
.
.
.
.
.
.
In the past I've used to print the "up arrow character" ASCII code. Can this be done in cscript?
过去我习惯打印“向上箭头字符”ASCII 代码。这可以在cscript中完成吗?
ANSWER
回答
Print on the same line, without the extra CR/LF
在同一行上打印,无需额外的 CR/LF
for a=1 to 15
wscript.stdout.write a
wscript.stdout.write chr(13)
wscript.sleep 200
next
回答by naivnomore
Use WScript.StdOut.Write()
instead of WScript.Print()
.
使用WScript.StdOut.Write()
代替WScript.Print()
。
回答by Tomalak
WScript.Print()
prints a line, and you cannot change that. If you want to have more than one thing on that line, build a string and print that.
WScript.Print()
打印一行,你不能改变它。如果您想在该行上包含多个内容,请构建一个字符串并打印它。
Dim s: s = ""
for a = 1 to 10
s = s & "."
REM (do something)
next
print s
Just to put that straight, cscript.exe
is just the command line interface for the Windows Script Host, and VBScript is the language.
直截了当地说,cscript.exe
它只是 Windows 脚本宿主的命令行界面,而 VBScript 是语言。
回答by Michael Erickson
I use the following "log" function in my JavaScript to support either wscript or cscript environment. As you can see this function will write to standard output only if it can.
我在 JavaScript 中使用以下“日志”函数来支持 wscript 或 cscript 环境。正如你所看到的,这个函数只有在可以的情况下才会写入标准输出。
var ExampleApp = {
// Log output to console if available.
// NOTE: Script file has to be executed using "cscript.exe" for this to work.
log: function (text) {
try {
// Test if stdout is working.
WScript.stdout.WriteLine(text);
// stdout is working, reset this function to always output to stdout.
this.log = function (text) { WScript.stdout.WriteLine(text); };
} catch (er) {
// stdout is not working, reset this function to do nothing.
this.log = function () { };
}
},
Main: function () {
this.log("Hello world.");
this.log("Life is good.");
}
};
ExampleApp.Main();