November 21, 2011

Vbscript - Pass Dos-Like Arguments to Your Script for Flexibility and control

Developers who have used Dos tools will be customary with the "switches" used to furnish Arguments or used as Arguments themselves. For example, listing the contents of a directory without any header information:

dir /ad /b

Variable Frequency Drive Basics

Or supplying data with "switches" like the drive letter:

doublespace /mount c:

Arguments are often passed to (Vbscript) scripts in a set order. The writer of the script perforce has to assume that the user of the script will furnish All of the ready Arguments in the precise order. We can enhance upon the way Arguments are passed to scripts by using switches within our seminar line.

The benefits of passing switches are:

  • Arguments can be passed in any order.
  • Non-essential Arguments can be left out.
  • We can be obvious in testing obvious Arguments pass particular criteria.
  • There is clarity within the actual seminar line being passed development it easier to read and understand.

As an illustrative example let's originate the starting of a script that's purpose is to crusade a file for particular words and write results out to the Command Line Interface (Cli). Our Arguments will be:

  • File name.
  • Switch [/W[W1,W2,W3...Wn]]: List of words to crusade for.
  • Switch [/C]: A count of the amount of times we find the passed in words.
  • Switch [/U]: To return a unique list of words found.

The absence of a switch for the filename will forewarn our script that the seminar is a filename.

Arguments passed to a script are held within the wscript.Arguments object. To access these Arguments we need to first instantiate an Arguments object:

dim command_line_args

set command_line_args = wscript.Arguments

We can now process the Arguments passed to our script. Let's call our script SearchLight.vbs and assume that the following Arguments are passed:

SearchLight.vbs /U /W boat,green,sailing BoatSales.csv

Our script's purpose is to return a unique list of green pilotage boat names from BoatSales.csv. We start by iterating straight through the Arguments passed and capturing the criteria. Though the Arguments object is an object with a Count method, we retrieve its Arguments starting at zero: Like an array. We'll make reading the code easier by adding a Constant called First_Item, which is set to zero, and a Variable called Last_Item which will capture the index amount of the last seminar held by our instantiated Arguments object.

const First_Item=0

dim Last_Item

dim args_index

dim command_line_args

set command_line_args = wscript.Arguments

'Check if any Arguments have been passed to our script.

if command_line_args.count > 0 then

Last_Item = command_line_args.count - 1

for args_index = First_Item to Last_Item

next

end if

Processing the passed Arguments is simplicity in itself. Because all but one seminar is tagged with a switch our script can recognise Arguments in any order. We will use a adopt Case statement to extract our Arguments into Variables.

select case (command_line_args(args_index))

case "/C"

'User only wants to return a count of the amount of boat names found.

count_only = true

case "/U"

'User wants a unique list of boat names found.

unique_name_listing = true

case "/W"

'Boat names must belong to boat records matching these criteria.

'Increment our Arguments counter by one to capture the next seminar with is a list of words.

args_index = args_index + 1

'Split the word list by the comma delimiter returning an Array of words to crusade for.

criteria = split(command_line_args(args_index),",")

case else

'assume a file name passed.

file_name = command_line_args(args_index)

end select

The most intelligent seminar is the one that provides a list of words to crusade for. The "/W" switch alerts our script that a list of words is to consequent and therefore we increment the Arguments index by one to capture that list. The next intelligent seminar is the "/C" seminar because it wasn't passed. When an seminar fails to match any of the conditions listed in our adopt Case statement our program assumes that it is a filename. It is the only assumption ready so it can be confidently acted upon.

Our full script looks like below:

const First_Item=0
dim Last_Item
dim args_index
dim count_only: count_only=false 'Default of false instantiates variable
dim unique_name_listing: unique_name_listing=false 'Default of false instantiates variable
dim criteria
dim file_name
dim command_line_args
set command_line_args = wscript.Arguments

'Check if any Arguments have been passed to our script.
if command_line_args.count > 0 then

Last_Item = command_line_args.count - 1

for args_index = First_Item to Last_Item

select case (command_line_args(args_index))

case "/C"

'User only wants to return a count of the amount of boat names found.

count_only = true

case "/U"

'User wants a unique list of boat names found.

unique_name_listing = true

case "/W"

'Boat names must belong to boat records matching these criteria.

'Increment our Arguments counter by one to capture the next seminar with is a list of words.

args_index = args_index + 1

'Split the word list by the comma delimiter returning an Array of words to crusade for.

criteria = split(command_line_args(args_index),",")

case else

'assume a file name passed.

file_name = command_line_args(args_index)

end select

next
end if

'--- file processing script ---

It's a simple, efficient and flexible technique. Let's look at how we would call this script via the cscript.exe via the Cli:

cscript //nologo SearchLight.vbs /U /W boat,green,sailing BoatSales.csv

Another benefit is that our script can test that all "essential" Arguments have been passed. Consider the above code. Our script can test the Variables count_only and unique_name_listing to ensure that at least one is set to True. If both are set to False, the script can throw an error.

Just one more idea to make our script a itsybitsy more elegant and polished. We could furnish a [/?] switch that tells our script to print its Switches and their use to screen. Holding consistency with Dos help applications we could print the following help text to the Cli:

cscript //nologo SearchLight.vbs [/C] [/U] [/W[W1,W2,W3...Wn]] [drive:][path][filename]
Searches Csv Boating sales files for records based upon passed in word critera.

/C Returns a count of the amount of records found.
/U Returns a unique listing of names of boats sold.
/W List of comma separated words that must exist within each record.
... And so on.

Using the conception of switches within our scripts will furnish us with added flexibility and operate when handling Arguments.

Vbscript - Pass Dos-Like Arguments to Your Script for Flexibility and control

Heating and Cooling Services