![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Our previous article on windows scripts was about a simple visual basic script (VBS) that would recursively list the contents of a folder including its subfolders, which I repeat here:
This script is fixed and will only list the contents of a single folder C:\TEMP so it isn't very useful. The problem is that we have fixed the folder name in the script, but there is an easy way out with command line arguments. Instead of specifying the folder to browse in the script, we pass it as an argument when we call the script, e.g.
This idea passing information from the command line will be familiar to programmers (e.g. main(argc, argv) in C language) and to people who did some work with old style DOS scripts. In VBS we can use the Arguments property to access this extra information within our script. Here is a test:
Notice that Arguments is an array property, and each item is accessed with a 0-based index (0 is the first element, 1 is the second...). The total number of arguments is in Arguments.Count. Armed with this information it is simple to modify the original script so that it reads the folder name from the command line:
See how we use WScript.Arguments(0) to specify which folder to read. Also noteworthy is the validation of command line parameters. If the caller doesn't supply any folder name, then the Count property is 0 and we display an error message and quit. Using scripts with FileSystemObject and Scriptable Shell Objects you can do pretty much everything you can imagine, just experiment and the sky will be your limit.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
© 2002—2010 Nikos Bozinis, all rights reserved |