Posted by: Jerry Lees
commandline, VBScript, vbscript tips, Wscript.Arguments
One thing that comes up frequently in my need to script tasks, is the ability to run a script from the command line, or as a scheduled job with parameters passed to it from the calling application. In a “set it and forget it” script it’s not such a big deal to have arguments passed to it, simply because you are likely to only use the script for one task… it will do it’s work and (hopefully) you never have to deal with it again. These are the best scripts! Ones where you save time every day when they run, and if they break you have to try to not only remember what they did, but where you scheduled them.
The first time I wrote such a beast I changed the script several times until I had a couple copies doing the same task on different directories. I then tried to create a script that ran against all the directories, but that too became a pain when I wanted to run it as a one off or run it on a different schedule.
Then I figured out how to add command line arguments to my scripts! What a powerful and time saving piece of information that was to figure out!
Command line arguments in VBScript are really very simple, once you have the concepts down and know what object to use. Which, of course, is the Wscripts.Arguments object! With this object you can pass any number of arguments and parse them in your script or add them into a variable– or anything you want, the sky’s the limit!
Enough rambling on though, lets get to the code that will save you a ton of time!
args = WScript.Arguments.Count
If args < 1 then
WScript.Echo “usage: args.vbs argument [argument] [argument] [argument] ”
WScript.Echo “You entered ” & args & ” Arguments, the first was “_
& chr(34) & WScript.Arguments.Item(0) & Chr(34)
As always, this code works perfectly. However, sometimes the formatting of the blog breaks the code if you copy and paste it into your editor. So, if you’d like to not type or troubleshoot any syntax errors due to the copy and paste problems– I’ve provided the code for download, plus example output files from my final tests for you. You’ll find the code and other files available for download from my website’s (www.websystemsadministration.com) File Depot under the ITKE Blog Scripts category. Enjoy and happy scripting!