Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - Handling User Input in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Handling User Input in WSH
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2007-02-19

    Table of Contents:
  • Handling User Input in WSH
  • Command Line Arguments
  • Using Named Arguments
  • Prompting Users in Cscript
  • Prompting Users in Wscript
  • Wscript Examples

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Windows Scripting Articles
    More By Nilpo/Developer Shed Staff Writer


       · Being able to handle user input gives your scripts a level of flexibility you can't...
     

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT