Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - 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 - Using Named Arguments


    (Page 3 of 6 )

    Sometimes it’s just not feasible to worry about what order the command line parameters are given in. Often there are optional command lines that can be left out by the user. This can make it very difficult to parse the arguments without writing a lot of extra code. Thankfully, WSH provides a way to make this a little easier.

    object.Named

     

    object.Item(Name)

    WSH allows us to use a named argument. The Named property of the Arguments object returns a dictionary of named arguments. The Item method is used to grab those dictionary entries according to the supplied Name. Let’s write a script that accepts two command line parameters: a name and an age. The command line might look like this:

    C:>cscript.exe namedargs.vbs /name:Nilpo /age:26

    The command line above demonstrates the use of named arguments. Each argument is listed as a command line switch with a name-value pair. The name and values are separated by a colon. Don’t forget to surround the names or values in double quotes if they contain spaces. Now let’s take a look at the code.

    Set dicNamedArgs = Wscript.Arguments.Named

    strName = dicNamedArgs.Item("name")

    strAge = dicNamedArgs.Item("age")

    Wscript.Echo "Your name is " & strName & ". You are " & strAge & "."

    What we’ve done here is quite simple. We’ve assigned the variable dicNamedArgs to our dictionary object. Then we call for each of the command line Items. If we run the example command line above, our output should look like this:

    Microsoft (R) Windows Script Host Version 5.6

    Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

     

    Your name is Nilpo. You are 26.

    Now try this example again with the same code. This time we’ll switch the order of the command line arguments. Because we’re calling by name, and not by order, our script will run perfectly.

    C:>cscript.exe namedargs.vbs /age:26 /name:Nilpo /uselesscommand:value

    Microsoft (R) Windows Script Host Version 5.6

    Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

     

    Your name is Nilpo. You are 26.

    Perfect! We got exactly the same output. Notice that it even ignored the extra command that it didn’t recognize. It’s also important to note that if two arguments have the same name, only the first will be processed. Any remaining arguments sharing that name will simply be ignored.

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