Getting StartedTo get started, click on Start -> Run and type in cmd to open the command line. At the prompt type cscript and you should see something like the output below
Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001. All rights reserved. Usage: CScript scriptname.extension [option...] [arguments...] Microsoft (R) Windows Script Host Version 5.6 Copyright (C) Microsoft Corporation 1996-2001. All rights reserved. Usage: CScript scriptname.extension [option...] [arguments...] Options: //B Batch mode: Suppresses script errors and prompts from displaying //D Enable Active Debugging //E:engine Use engine for executing script //H:CScript Changes the default script host to CScript.exe //H:WScript Changes the default script host to WScript.exe (default) //I Interactive mode (default, opposite of //B) //Job:xxxx Execute a WSF job //Logo Display logo (default) //Nologo Prevent logo display: No banner will be shown at execution time //S Save current command line options for this user //T:nn Time out in seconds: Maximum time a script is permitted to run //X Execute script in debugger //U Use Unicode for redirected I/O from the consoleIf you got this far then Windows Scripting Host is working properly on your machine and we can get started with coding.
Setting up the environmentCreate a new folder called idleworx_scripts (or whatever) in your root c:\ folder.
1. Open notepad and enter the code below
2. Save the file as hello_world.wsf in the folder you created about (eg. c:\idleworx_scripts\hello_world.wsf)
3. Open the windows command line and navigate to the folder c:\idleworx_scripts
4. Run the program by typing cscript hello_world.wsf
5. You should see Hello World printed on the command line.Congrats, you just ran a very basic script using Windows Scripting Host
- Notice that to run a script using Windows Scripting Host you must put it in a *.wsf file. This allows the Windows Scripting Host to interpret it as an xml file and provide some useful parameters to your program as you’ll see later in this tutorial
- There is a way to call a *.js file directly as well [tutorial some other time]
and elements are required in the file. The <job> element allows you to separate your files into several programming modules.
- To print something to the console, you call the WScript.Echo(). ('WScript' is one of the 3 standard objects which Windows Scripting Host provides. The other being 'WshShell' and 'WshNetwork')
Hello World (with arguments)
Let's expand on the hello world example by using arguments. Arguments can be passed to your program from the command line by using a syntax similar to this:
By using Windows Scripting Host’s built in argument support you can make sure you users specify certain arguments as well as showing them how to properly call your script, as you will see shortly.
Let's make some changes to our original hello world program.
1. Open notepad and make the following changes to the hello_world.wsf file:
2. Now run the program with the command line:
cscript hello_world.wsf /name:John /age:25
3. You should see the output below:
Hi john you are 22 years old.
First we have added a
section to the program. This allows us to define parameters which can be passed from the command line to the program. We’ve defined a 'name' and an 'age' parameter, as well as provided a 'description' of what this program does.
- You’ll notice that we’ve changed the structure of the script part a bit. We created a main() method which will be the starting point of the script. This is setup is easier to work with.
- In order to read the arguments from the command line we loaded them in a variable called oArgsvar oArgs = WScript.Arguments.Named;
- In the following section we also check to see if the user hass passed in any argumentsif(oArgs!=null && oArgs.length>0) and if so display a welcome message using the arguments. Notice here that our code checks to see if any arguments have been passed in, it doesn’t validate for each one, so a user can just enter his age and his name will be left as ‘undefined’. You can now hopefully figure out how to add further validation to your arguments.
If the user hasn't passed in any arguments, we use the WScript.Arguments.ShowUsage() line to tell the user which arguments to pass in. This uses Windows Scripting Hosts' built in functionality for displaying the usage of a program (which we defined in the runtime section).
So if you call the program without any parameters:
You should see the following output:
Hopefully this all makese sense and you can see how easy it is to write basic programs with and without parameters.
To make things easier in the future copy the code below and use it as a template for any wsf file you want to run