The VBScript Network and Systems Administrator's Cafe

Sep 12 2008   6:04AM GMT

How to use command line arguments in VBScript via the Wscript.Arguments Object

Jerry Lees Jerry Lees Profile: Jerry Lees

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.Quit
end If

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!

1  Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Bearbreath
    Your command line arguments in VB script were verry interesing. I have an amateur radio pdrogram that requires UDP Ports 5198 and 5199 to be open in order to make this program work. I keep losing these ports. I built a dual core computer with the AMD CPU 6000+. I use Win Vista Ultimate. I was wondering if some one can write me a VB script that would set these ports up without such a hassle. I enjoy your E-Mail. Ken Skinner k7inm@verizon.net
    10 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: