Windows XP Annoyances Free Open Book

Windows XP Annoyances

Previous PageNext Page

9.8 Using Command-Line Parameters in Scripts

A command-line parameter is a bit of text specified after the filename of a script when it is executed from a command prompt (see the following examples). The function used to convert a single command-line parameter into a variable is the following:

Function CommandLine(Number)
  Set Arguments = WScript.Arguments
  If Number <= Arguments.Count Then 
    CommandLine = Arguments(Number - 1)
  Else
    CommandLine = ""
  End If
End Function

For example, to display the second command-line parameter passed to a script, issue the following statement:

MsgBox CommandLine(2)

Although the command line may seem to be an antiquated concept, it's still very much a part of Windows. When you double-click on a .vbs file, for example, Windows actually executes the following command:

wscript.exe filename.vbs

where filename.vbs (the file that was double-clicked) is the command-line parameter for wscript.exe, telling it which script to run. Scripts also accept command-line parameters, which is accomplished like this:

wscript.exe filename.vbs param1 param2

The two additional parameters,[2] param1 and param2, are both passed to the script as command-line parameters, and can be retrieved during run-time by referencing CommandLine(1) and CommandLine(2), respectively.

[2] You can have as many or as few parameters as you like.

One of the most common uses of command-line parameters in scripts is to accept filenames, and there are two circumstances when this is most useful:

  • Drag one or more items onto the script file icon. Note that this didn't work in earlier versions of Windows, as scripts were considered to be documents instead of programs.

  • Place the script in your Send To folder. Then, right-click one or more items in Explorer, select Send To, and then select the name of the script. You can also place the a shortcut to the script in your Send To folder, which eliminates the .vbs filename extension that would otherwise appear in the Send To menu.

In either case, the script is executed, and the names of the input file(s) are accessible as command-line parameters, one for each filename. The following example script displays the names of all the files and folders drag-dropped on the script icon:

Report = ""
Set Arguments = WScript.Arguments
For i = 1 to Arguments.Count
  Report = Report + Arguments(i - 1) + vbCrLf
Next
Msgbox Report

The script starts off by clearing the Report variable, and then borrows some code from the CommandLine function listed earlier[3] to initialize the Arguments object and determine the number of dropped files. Next, a For...Next structure is used to run through the arguments, adding each one to the Report variable, followed by a linefeed (using vbCrLf, a handy built-in constant containing carriage-return and linefeed characters). Note that the Arguments array is zero-based (the first item is Arguments(0), the second is Arguments(1), and so-on), so we need to include the (i - 1) part to compensate. Lastly, a Msgbox command is used to display the list of dropped files.

[3] It's actually possible to use the CommandLine function here instead, but doing so would make the script more cumbersome. And exactly who are you going to impress with a cumbersome script?

Previous PageNext Page


     Main Menu
Cover
Copyright
Preface
Chapter 1. Making the Most of Windows XP
Chapter 2. Basic Explorer Coping Skills
Chapter 3. The Registry
Chapter 4. Tinkering Techniques
Chapter 5. Maximizing Performance
Chapter 6. Troubleshooting
Chapter 7. Networking and Internetworking
Chapter 8. User Accounts and Administration
Chapter 9. Scripting and Automation
9.1 Building a Script with VBScript
9.2 Running Applications from Scripts
9.3 Accessing the Registry from Scripts
9.4 Manipulating Files from Scripts
9.5 Creating Windows Shortcuts and Internet Shortcuts in Scripts
9.6 Networking with Scripts
9.7 Manipulating Internet Explorer from Scripts
9.8 Using Command-Line Parameters in Scripts
9.9 Managing Services with Scripts
9.10 Writing CGI Scripts for a Web Server
9.11 Making a Startup Script
9.12 Deciphering Script Errors
9.13 Finding a Better Editor
9.14 Further Study
9.15 Automating Scripts with Scheduled Tasks
9.16 Wacky Script Ideas
Chapter 10. Installing Windows XP
Appendix A. Setting Locator
Appendix B. BIOS Settings
Appendix C. Command Prompt Crash Course
Appendix D. TCP/IP Ports
Appendix E. Error Messages (Blue Screen of Death)
Colophon


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele