Handling User Input in WSH - Prompting Users in Cscript
(Page 4 of 6 )
Maybe you want to prompt a user based on information gathered by the script. Or perhaps it’s just not feasible to use command line parameters. In either case, it’s sometimes useful to prompt for user input during the execution of a script. In Cscript this is done using StdIn and StdOut.
Those familiar with command line programming know that StdIn (standard input) and StdOut (standard output) are input and output in a command window, respectively. WSH makes these accessible through Wscript properties by the same name.
object.StdIn
object.StdOut
The StdIn property returns a read-only TextStream object from the Standard Input stream. The StdOut property returns a read-only TextStream object from the Standard Output stream. Both of these are only available when using the Cscript host.
All of the TextStream object methods and properties are available to us, but we’ll only focus on the most commonly used ones for our purposes.
Methods
object.ReadLine
object.Write(String)
Properties
object.AtEndOfStream
You should be familiar with these methods and properties from my Working With Text Files series, but here’s a quick recap. The ReadLine method returns a string containing the contents of the TextStream object. The AtEndOfStream property is available at every character position and returns true when the end of the text stream is reached. The Write method writes a supplied string to a TextStream object.
Before we can get a user’s input we first need to tell them what we want. We’re going to prompt them by writing to the command window’s standard output stream. This is done by connecting to the StdOut object. We then accept their input from the standard input stream by means of the StdIn object.
Wscript.StdOut.Write "What is your name? "
strName = Wscript.StdIn.ReadLine
Wscript.StdOut.WriteLine "You have indicated that your name is " & strName
This simple snippet will produce a prompt asking for the user’s name. Below is a sample output of what this script might look like when executed.
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
What is your name? Nilpo
You have indicated that your name is Nilpo
We can expand our example a little to show that these text strings can be manipulated just like any other strings. I’ll show you the difference between using the Echo method and StdOut.
Wscript.StdOut.Write "What is your name? "
strName = Wscript.StdIn.ReadLine
Wscript.Echo "How old are you? "
strAge = Wscript.StdIn.ReadLine
Wscript.StdOut.WriteLine "Your name is " & strName & ". You are " _
& strAge & "."
Here we’ve changed the code to also prompt for an age. Notice that the age prompt uses the Echo method rather than the StdOut object. Here’s how the output looks now.
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
What is your name? Nilpo
How old are you?
26
Your name is Nilpo. You are 26
You can see that I entered my age on the line beneath the age prompt. That’s because the Echo method always appends a string with a linefeed character which left the cursor on the next line.
Next: Prompting Users in Wscript >>
More Windows Scripting Articles
More By Nilpo/Developer Shed Staff Writer