cscript - 在控制台上的同一行打印输出?

时间:2010-05-24 14:29:29

标签: windows ascii console-application wsh control-characters

如果我有一个cscript输出到屏幕的行,我怎么在每次打印后避免“换行”?

示例:

for a = 1 to 10
  WScript.Print "."
  REM (do something)
next

预期输出应为:

..........

.
.  
.
.
.
.
.
.
.
.

过去我曾经打印过“向上箭头字符”ASCII码。这可以在cscript中完成吗?

答案

在相同的行上打印,没有额外的CR / LF

for a=1 to 15
  wscript.stdout.write a
  wscript.stdout.write chr(13)
  wscript.sleep 200
next

3 个答案:

答案 0 :(得分:11)

使用WScript.StdOut.Write()代替WScript.Print()

答案 1 :(得分:2)

WScript.Print()会打印一行,但您无法更改。如果你想在那一行上有多个东西,建立一个字符串并打印出来。

Dim s: s = ""

for a = 1 to 10
  s = s & "."
  REM (do something)
next

print s

简而言之,cscript.exe只是Windows脚本宿主的命令行界面,而VBScript就是语言。

答案 2 :(得分:-1)

我在JavaScript中使用以下“log”函数来支持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();