Windows Scripting
  Home arrow Windows Scripting arrow Page 6 - 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 - Wscript Examples


    (Page 6 of 6 )

    Let’s go back to one of our Cscript examples and rewrite it for Wscript.  First, we’ll ask for the user’s name.  Then, we’ll ask for their age.  We’ll finish up by showing our results.

    strName = InputBox("What is your name?", "About You", _

       "Enter your name")

    result = MsgBox("You have indicated that your name is " & strName _

       & vbCr & "Is this correct?", VBYesNo + VBQuestion, _

       "Please check your name")

    strAge = InputBox("How old are you?", "About You", "Enter your age")

    result = MsgBox("Your Name is " & strName & ". You are " & strAge _

       & ".",, "Your Results")

    This produces a series of input and message boxes to gather the user’s name and age.  It finishes by displaying the results.  There are a few things to note in this example.  First, notice the two different ways we’ve called the MsgBox function.  I’ve used a mixture of the optional attributes.

    You should also notice the last message box.  I’ve used the optional Title attribute but have not used the Button attribute.  I included an extra comma to show where the Button attribute would have been.  Even though the Button attribute is missing, the Title attribute must still be the third attribute passed to the function.

    The first message box is used check that the name is correct.  It doesn’t do much good if we don’t do something to handle the result.

    strName = InputBox("What is your name?", "About You", _

       "Enter your name")

    result = MsgBox("You have indicated that your name is " & strName _

       & vbCr & "Is this correct?", VBYesNo + VBQuestion, _

       "Please check your name")

    If result = VBNo Then

       MsgBox("Sorry. Please try again.", 16, "Program Terminated")

       Wscript.Quit

    End If

    strAge = InputBox("How old are you?", "About You", "Enter your age")

    result = MsgBox("Your Name is " & strName & ". You are " & strAge & _

       ".",, "Your Results")

    I’ve added an If statement to check the result of the first message box.  If the No button was pressed it notifies the user and ends the script.  Notice that I’ve used the exact values to call this message box.

    Although you can call the MsgBox function using either constants or values for the Button attribute you should maintain a sense of continuity in your script by using the same method throughout.

    We should also add some code in case our user presses the Cancel button on any of our input boxes.  To avoid stretching this code sample on forever I’m going to make use of a subroutine.

    strName = InputBox("What is your name?", "About You", _

       "Enter your name")

    If strName = "" Then Call UserCancel

    result = MsgBox("You have indicated that your name is " & strName _

       & vbCr & "Is this correct?", VBYesNo + VBQuestion, _

       "Please check your name")

    If result = VBNo Then

       result = MsgBox("Sorry. Please try again.", 16, _

           "Program Terminated")

       Wscript.Quit

    End If

    strAge = InputBox("How old are you?", "About You", "Enter your age")

    If strAge = "" Then Call UserCancel

    result = MsgBox("Your Name is " & strName & ". You are " & strAge & _

       ".",, "Your Results")

     

    Sub UserCancel

       result = MsgBox("You have chosen to end this program.", _

           vbInformation, "Program Terminated")

       Wscript.Quit

    End Sub

    If you’re using a message box that has multiple buttons, it may be easier to use a Select statement to check your answers.

    It’s possible to use a Select statement to process message box results because the result constants actually represent the integer values.

    Here’s a quick example of what that might look like.  Try running it a few times and press each of the possible buttons.

    result = MsgBox("Press a button", VBAbortRetryIgnore)

    Select Case result

       Case VBAbort

           Wscript.Echo "You pressed Abort"

       Case VBRetry

           Wscript.Echo "You pressed Retry"

       Case Else

           Wscript.Echo "You pressed Ignore"

    End Select

    Now you’ve seen how to handle user input in both Cscript and Wscript using command line parameters when calling your script and by prompting for the information during execution.  This can add a whole new dimension to your scripting practices.  Thanks for reading once again.  Until next time, keep coding!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 2 hosted by Hostway
    Stay green...Green IT