Fun VBScript tip

When writing a VBScript designed to run at a command prompt (or "console") you some times want to not have to write data to a new line.
 
For instance, you might want to write a "." each time you loop through a array or collection so that you know something is happening.  Using WScript.Echo each use generates a CR\LF, much like using the vbCrLf constant or the textstream.writeline method.
 
This is where the WScript.StdOut.Write method comes in handy.  While not well documented this method works exactly like we need it to.  Take the following example where rsNames is a result set from an ADO query:
 
 
 
Do While Not rsNames.EOF
  If rsNames.Fields.Item(0).Value = "Bob" Then
    ‘ Write a period to show work then start a new line
    WScript.StdOut.WriteLine "."
    Wscript.StdOut.WriteLine "I found Bob!"
    Exit Do
  Else
    ‘ Write a period to show work
    Wscript.StdOut.Write "."
  End If
Loop
 
And if you really want to have some fun you could go for the "rotating line":
 
a = 0
b = 10000000
c = "\"
do while a < b
  if c = "\" then
    c = "|"
  elseif c = "|" then
    c = "/"
  elseif c = "/" then
    c = "-"
  elseif c = "-" then
    c = "\"
  end if
  wscript.stdout.write chr(08) & c
  wscript.sleep(100)
loop
 
Just for clarification chr(08) is a backspace and the sleep for 100 milliseconds makes the rotation direction of the line more obvious. : )
 
Have fun!
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s