cscript - print output on same line on console?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 04:11:45

问题


If I have a cscript that outputs lines to the screen, how do I avoid the "line feed" after each print?

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?

ANSWER

Print on the same line, without the extra CR/LF

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

回答1:


Use WScript.StdOut.Write() instead of WScript.Print().




回答2:


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.

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.




回答3:


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.

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();


来源:https://stackoverflow.com/questions/2897698/cscript-print-output-on-same-line-on-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!