Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - 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 - Command Line Arguments


    (Page 2 of 6 )

    Let’s take a look at how to handle command line parameters that are supplied when calling a script.  The user will simply append some information to the command line at the time of execution.

    Multiple command line parameters are separated by spaces so any argument that contains spaces should be enclosed in quotation marks.

    Let’s create a very simple script that accepts a text string parameter.  We’ll simply Echo the text string back to the user.  Copy the following text into any text editor such as Notepad and save it as echoscript.vbs.

    Set colArgs = Wscript.Arguments

    Wscript.Echo colArgs.Item(0)

    This script simply assumes that there will be only one item in the colArgs collection.  This string is returned using the Echo command.  Let’s see how it works by taking a look at the following command line and results.

    C:>cscript.exe arguments.vbs “Hello World!”

    Microsoft (R) Windows Script Host Version 5.6

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

     

    Hello World!

    There you have it.  Our string was echoed back exactly as expected.  What would happen if we tried this without the required quotation marks?

    C:>cscript.exe arguments.vbs Hello World!

    Microsoft (R) Windows Script Host Version 5.6

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

     

    Hello

    Oops!  Cscript encountered a space and processed the words as two separate parameters.  Our script only echoes the argument located at Item 0.  In other words, it will only return the first word.

    So it’s obvious at this point that we need to process each of these arguments individually.  We’ll do that by creating a loop.

    Set colArgs = Wscript.Arguments

    For i = 0 To colArgs.Count - 1

       strText = strText & colArgs.Item(i) & " "

    Next

    Wscript.Echo strText

    As you can see we’re looping through each item based on its index value.  We used the Count method to determine how many times our loop needs to iterate.  Don’t forget that indices are zero-based so our highest index value is our Count minus one.

    There is one other important step that you should add to your scripts when dealing with arguments.  What happens if the user doesn’t supply the required arguments?  A little error-handling can both notify the user and also prevent unexpected errors.

    If Wscript.Arguments.Count Then

       Set colArgs = Wscript.Arguments

       For i = 0 To colArgs.Count - 1

           strText = strText & colArgs.Item(i) & " "

       Next

       Wscript.Echo strText

    Else

       Wscript.Echo "This script cannot be run without parameters."

    End If

    The Arguments object will always exist even if it is empty so we can build a simple If statement around the Count method.  A count of 0 will be considered False while any other value will be considered True.

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