12

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
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Guy
  • 9,720
  • 7
  • 38
  • 42

3 Answers3

15

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

Cellcon
  • 1,245
  • 2
  • 11
  • 27
naivnomore
  • 1,291
  • 8
  • 14
  • oops - regredded to my VB days. Yes, WScript.Print is the correct command! – Guy May 24 '10 at 14:57
  • 1
    I meant you could use wscript.stdout.write instead of wscript.print to print on the same line without the new line characters. – naivnomore May 25 '10 at 17:17
  • Note that this will fail when run in [wscript](https://stackoverflow.com/a/9062764/1026), you might want to make sure [the script is running in console](https://stackoverflow.com/a/4572553/1026). – Nickolay Mar 28 '19 at 01:49
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.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Yes, wscript.print is right - regressed to my old VB script days... I'm sure you know you can "echo" command characters to the console and this is how you wrote the old "DOS" style applications. Can this still be done to manipulate the cursor? – Guy May 24 '10 at 15:01
  • @Guy: VBScript's `WScript.Print()` works like VB6's `Debug.Print()` in regard to newlines, so... no, not to my knowledge. – Tomalak May 24 '10 at 15:56
-1

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();
Michael Erickson
  • 4,217
  • 2
  • 23
  • 10