This is a big advantage over batch files, which are restricted to using shared environment variables, because it eliminates potential conflicts that can occur between scripts.. If you ne
Trang 1WSH provides the capability for you to create and manipulate variables within your
scripts Unlike batch files, variables are constrained so they are only available within a single script during the execution of the script This is a big advantage over batch files, which are restricted to using shared environment variables, because it eliminates
potential conflicts that can occur between scripts Furthermore, variables within WSH scripts can take any type of value including strings, numbers, and objects In batch files,
If you’ve used batch files before, you might wonder how you can translate
your batch files into their WSH equivalent so that you can take advantage of
the enhanced capabilities of WSH In this section, you’ll take a look at batch
As you go through this section, you’ll notice that some of the WSH
equivalents for batch file functions are much longer in terms of
lines of code That’s one of the few advantages of batch files over
WSH scripts You might find that for certain tasks it’s easier for you
to implement batch files.
First, you’re going to take a look at some of the language constructs that are
used in batch files and the equivalent language constructs in WSH It’s
important to keep in mind that these language constructs are dependent on
If you want to use variables in batch programs, you must use environment
variables Here’s an example that shows how you might use environment
Trang 2When you’re using scripts with WSH, you don’t need to use environment
variables That’s much safer, and you don’t need to clean up the variables at
the end of your WSH script Here’s a short example that shows how you can
use VBScript to create a variable:
As you can see, the JScript code is actually somewhat shorter than the
VBScript equivalent In any case, like VBScript, you don’t need to clean up
One thing that you can do with variables in WSH scripts is set them to values
that are more than just strings The following example shows how you can
When you use WSH variables, you have very few limitations You also don’t
need to worry about collisions between variables that you use and variables
that exist in other scripts.
to evaluate logical conditions and branch accordingly Here’s a batch file
Trang 3IF %MY_VAR%.==.ValueA GOTO WasValueA
That works okay when you have a simple condition If you have a more
complex condition, batch files quickly become messy The following example
is very similar to the first but includes a more complex condition:
Trang 4’ Create a variable to use to illustrate the use of
The advantage of the two examples is that the flow of the program is simple
and easy to understand; the flow is certainly simpler and easier than the flow
of the batch file With the second batch file example, you can quickly see
how code turns into spaghetti code that can be very difficult to debug and
maintain Now, take a look at the equivalent of the second batch file in
Trang 5Select Case my_var
That example demonstrates two different ways that you can perform
branching logic in VBScript Now take a look at similar functionality using
You can see that the JScript example is very similar to the VBScript
example; the syntax varies a little Both examples are much better structured
than the batch file example If you need to use complex logic, consider
changing your batch files to WSH scripts so that you can make use of the
In batch files there are two basic types of iteration; you can iterate over a
plain-Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com
Trang 6vanilla iteration using the GOTO statement The second type of iteration is
similar to the typical iteration that’s used in a program for a while or for
type of loop, but unfortunately it’s much weaker.
Typical batch files use GOTO in conjunction with IF statements to perform
that type of iteration:
The previous example shows a batch file loop that iterates through the
parameters for the batch file If the batch file is run with the command line
gotoloop IterationA IterationB IterationC IterationD, you
see the results in Figure 10.6.
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com
Trang 7The batch file iteration techniques are effective but very clunky They make it
easy to iterate over multiple files but difficult to iterate for a specific number
of times or until a complex condition is satisfied.
Some of these batch file restrictions have been eliminated with
Windows NT command extensions For example, the FOR
command is extended to perform iteration for a specific sequence
in Figure 10.7 There are also some additional enhancements with
the command extensions that make it possible to simulate
subroutines using batch files The extensions are very useful;
however, they are still not as flexible or anywhere near as powerful
Trang 8embedded loops However, this is one situation where the WSH equivalent is
not necessarily as simple or as short as the batch file version Iterating over
a group of files can be more complex but also more flexible using WSH
Here’s an example that uses VBScript to iterate over all the files in the
Windows NT system directory:
That’s not too complex, but it gets worse If you want to iterate over just a
specific type of file, like all the DLLs that begin with the letter e, you need to
implement additional file-checking logic Here’s an example that does the
Trang 9Figure 10.8: Iterating over files using VBScript.
Both of those examples show some of the excellent looping capabilities
provided by WSH, but they also show that sometimes WSH scripts might not
be as easy to implement as batch files Here’s another simple example that
illustrates the use of the while loop:
That’s obviously a very trivial example, but it does demonstrate complex
logic and comparisons that are much better than the capabilities provided by
When a batch file runs a command, the command might return a numeric
DOS error level Typically, an error level of zero represents success,
whereas a nonzero error level is a failure number Here’s an example that
text error and sets the DOS error level That’s more feedback than some
other commands; many set the error level without displaying a descriptive
use it to check for a specific error level; it checks for an error equal to or
greater than the error level that is specified.
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com
Trang 10extensions improve the batch file capabilities With the command
WSH scripts, assuming that you’re using VBScript, provide much more
robust error handling than batch files (Unfortunately, the current version of
object that provides properties that include a numeric error code, a
description of the error that occurred, and the source of the error You can
Here’s an example that illustrates the check for an error:
Trang 11The output of the script is shown in Figure 10.10 The script can easily be
Err.Number <> 0 Then line Because of the capabilities of WSH, you can
create scripts that notify users of problems and then enable them to retry
operations Here’s a sample script that uses retry logic to prompt a user to
enter a different filename if an error occurs:
’ An error occurred when we tried to get the
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com
Trang 12doesn’t exist When it fails, it displays a yes/no dialog box, shown in Figure
10.11, that enables the user to retry the operation.
If the user selects yes, he is prompted to enter a new filename The prompt
dialog box is illustrated in Figure 10.12 When he reenters the filename, the
Trang 13
This shows some of the power that is inherent in WSH error handling These
are just basic examples; you can easily extend them to perform additional
In this section, you’re going to examine some common techniques used in
batch files, and you’ll see how you can duplicate the functionality within your
WSH scripts For each section, you’ll follow the pattern that you used in the
previous section; first you’ll see an example for a batch file, and then you’ll
provides a syntax that enables you to perform an action if a file exists Here’s
statement Now, take a look at the equivalent functionality using WSH To
Obviously, because you’re using WSH, it is very easy for you to to
incorporate additional complex logic for the file check For example, you can
check the size of file, or, if the file is a DLL or an executable, you can check
internal file resources such as a version stamp In Day 11, "Administering
User Logins, Program Settings, Folders, and File Shortcuts Using WSH,"
you’ll see additional examples that illustrate how you can use the
FileSystemObject to check for files and perform other file operations.
Batch files can access nine command line parameters simultaneously The
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com
Trang 14name of the batch file that was called, and %1 through %9 are the command
parameters You can specify more than nine parameters; to access them,
Accessing command-line parameters is easier using WSH than it is using
batch files With WSH, you can easily access each command-line parameter,
and you can tell from the beginning how many parameters are specified for
the script Here’s a sample script that shows how you can access
If you’re converting a batch file to a WSH script, you can eliminate your
shift statements and just make use of the Wscript.Arguments property
With batch files, you have no choice for how you want to display a message
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com
Trang 15You can certainly do the same type of thing with WSH scripts You’ve used
That’s not all that you can do to display messages to the user using WSH
You can use internal functions or objects to display message boxes, or you
can even make use of external custom objects to display your own UI You’ll
see that technique demonstrated later in this book For now, here’s an
There are only two standard options for getting user input with batch files
Press any key to continue and wait for the user to press any
developed to provide more robust support for user input Unfortunately,
Choice was not designed for Windows NT 4.0 You can try to use it; beware
that it is not officially supported The following batch file uses the Choice
Trang 16is effective for batch files, but it is limited because it only enables the user to
make selections from a canned menu It also shows how weak the structure
of batch files must be because of the lack of good programming constructs
Trang 17Figure 10.13: A batch file menu using the Choice command.
Unfortunately, if you’re using JScript, there is no standard mechanism for
The result of this script is shown in Figure 10.14 The example shows both
strengths and weaknesses of the WSH support for user input The benefit of
InputBox() is that it enables you to capture any type of user input At the
constrain selections or to enable the user to select from a list With VB, Java,
C++, or any other language that enables you to create COM objects, you can
define custom objects that can supplement your WSH scripts with custom
perform drive mapping tasks For now, you’ll do a quick comparison that
Trang 18that enable you to create or remove drive mappings, among other things
provides all the networking functionality for WSH scripts If you want to find
Object Model." Here’s the equivalent WSH script:
Trang 19
This chapter began with a discussion of the history of batch files on the DOS/Windows platform Batch files were developed as an effective and simple solution for command automation They provide powerful but basic automation capabilities Relative to robust programming languages, batch files are weak and cumbersome However, there are
WSH equivalents for functionality that you might be using in batch files Next, you
examined several standard batch file usage scenarios The batch file scenarios were
In batch files, environment variables are the only place that you can store
temporary values Typically, batch files need to clean up environment
variables that they use before they terminate Do WSH scripts also need to
clean up variables that they use?
No, WSH scripts do not need to clean up variables that they use The internal
variables that are used by a script are only available while that script is executing;
Although batch files provide no debugging utilities, WSH scripts have
excellent debugging tools Name two tools that can be used to debug WSH
Trang 20
3. What object do you need to use in WSH if you want to check for the existence of a file?
Trang 21existing batch files with more robust and complete scripts developed using WSH Now you’re going to put some of those techniques and ideas into action with scripts that will
This chapter shows you how to create WSH login scripts, programmatically create
directory mappings (and check to make sure that directory mappings succeed), check for the existence of files and check file timestamps, administer program Registry
settings, and create shortcuts and directories By the time you are finished with this
chapter, you’ll see how you can use WSH for administration tasks, and you’ll have some
The WindowsScriptingHost is a generalized tool that allows you to perform a multitude
of administrative tasks You can use the objects that are built into WSH, such as the
WshNetwork or WshShell objects, to administer drive mappings or program settings
check files and add folders In this chapter, you'll see code examples that use those
objects and others for user administration.
In this context, user administration means to administer the user's computer, including
the Windows desktop, program settings, files, folders, and so on It does not mean that you will actually be creating or modifying users with WSH scripts That topic is
discussed in Day 18, "Using WSH to Manipulate the Microsoft Active Directory." As Day
administration tool because it can script any ActiveX objects Because many of the
Microsoft technologies expose ActiveX administration objects, they can be administered
no real programming constructs and certainly no capability to leverage COM objects
Now you can replace them with more robust WSH scripts that perform more functions With WSH scripts, you can perform many tasks and check the results much more easily
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com
Trang 22Directory mappings are typically done the old way using batch files When you map
drives, you use the net use command with the name of the server and share that you want to map to specified using the Universal Naming Convention (UNC) in the form
\\server\share For example, you may have a batch file that bitmaps to a directory using something like the following syntax:
The problem is that it can be difficult to check whether the directory mapping
succeeded With WSH scripts, you can do the directory mapping using the
WshNetwork object and check the result to ensure that the directory was mapped
With batch files, it’s literally impossible to check file versions There’s no good way,
unless you extend batch files with external programs or utilities, to check file versions
Later in this chapter, you’ll see how you can perform this task using scripts.
One major limitation of batch files is that they don’t provide any capability to change
the Windows Registry Later in this chapter, you use the WSH TextStream objects to
Trang 23This is one of the few examples where we will use the Microsoft JScript
language Current versions of JScript provide no support for error handling,
so it’s difficult to use them to develop robust scripts This will be fixed in
upcoming versions of JScript, which will support robust error handling using a try catch mechanism similar to the Java language.
To set up login scripts, go into the Windows NT User Manager for domains Select a
user, or multiple users, and press Enter, or select User, Properties from the menu You’ll see the dialog illustrated in Figure 11.2.
Trang 24Obviously, if Windows NT is installed on a different drive or in a different root directory, your path will be different It’s important to note that the aforementioned directory is also
You may get inconsistent results using the aforementioned method
Sometimes it appears that the user manager has problems resolving the
path to the script Consequently, consider wrapping your scripts in the
following batch file.
That can help you to eliminate any problems with directory dependencies Here’s a
sample batch file that you could use:
Users must have WSH installed locally to run WSH login scripts In the
last chapter of this book, we discuss how you can deploy WSH script
solutions and the files that you need to distribute to end users for them to
user, such as the username You can also use the name with other objects to perform
log in.
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com
Trang 25proactive in scripts to give the end user feedback about potential problems, or to alert
whatever is appropriate for your environment Now, let’s run a quick example that shows some of the usage of the object Before running the script, drives in the Explorer look like Figure 11.5.