Sunday, April 22, 2007

VBScript - Printing Results to the Commandline for Flexibility

by Duane Hennessy
Eliminating the ability of our scripts to write output directly to a file increases the flexibility in the ways our scripts can be used.

Often, when a script writes results to a file, the output file's path and name are hard-coded into the script or passed to the script as an Argument. Any further processing of the resulting data must read in data from a file and access the disk again. This limits our ability to use other Commandline tools forcing us to duplicate effort by writing Heap Sort routines instead of using DOS's Sort command and so on.

Eliminating the ability of our scripts to write output directly to a file increases the flexibility in the ways our scripts can be used. Our scripts should write any processed output to the Command Line Interface (CLI). For example, let's say we already have a script called Transform.vbs which transforms data in one file and sends the processed results to another file. Originally we may have had to provide both the input file and the output file as arguments (if we didn't hard-code the output file within our script).

cscript //nologo file_in.txt file_out.csv

So it does its job. But it really is WYSIAYG (what-you-see-is-all-you-get!). If our script wrote its processed results directly to the CLI then we could write the following:

cscript //nologo file_in.txt > my_file.csv

Seems like a small change doesn't it? But with the added flexibility we can now further process the results using DOS or other CLI tools. Suppose we wanted to sort the results A-Z aswell:

cscript //nologo file_in.txt | sort > my_file.csv

Perhaps we want to filter results based upon certain criteria and sort them A-Z:

cscript //nologo file_in.txt | find "my criteria" | sort > my_file.csv

We can do more. Say we have a file listing zoo animal details:

Cage=1, Animal=Monkey, Food=Bananas, Count=6
Cage=2, Animal=Giraffe, Food=Leaves, Count=2
Cage=3, Animal=Elephant, Food=Popcorn, Count=3
Cage=4, Animal=Prawn, Food=Fish flakes, Count=100
Cage=5, Animal=Lion, Food=Pedestrians, Count=20
Cage=6, Animal=Scorpion, Food=Chicken, Count=25
Cage=7, Animal=Otter, Food=Fish, Count=12

Our script processes this file working out the dollar amount of our food bill by accessing the prices of food on a database. We want a bill for all of the animals listed in order of their name. We do not want any animals which eat Fish or Fish flakes. We can process our data like this:

cscript //nologo file_in.txt | | find /V "Fish" | sort > my_file.csv

The result of our script processing the dollar cost and printing its CSV output is:


With a little imagination we can extend this to print out results via the Notepad application using Notepad's [/P] switch. So now we have:

cscript //nologo file_in.txt | | find /V "Fish" | sort > my_file.csv&sleep 3000¬epad /p my_file.csv

Here is an example where we process multiple files (We don't print them though):

for %i in (*.txt) do cscript //nologo %i | find /V "Fish" | sort >> my_file.csv

All this flexibility is available to us when we forgo writing directly to a file from our script and print results directly to the CLI instead.

No comments: