The output from the script is a text file called drives.log; you can see the results of running the script in The Drive object represents a disk drive or network share.. In the example f
Trang 1the system using the Drives collection The output from the script is a text
file called drives.log; you can see the results of running the script in
The Drive object represents a disk drive or network share To use a Drive
object, you must reference it from an instance of the FileSystemObject
You can’t instantiate it directly using CreateObject() This also holds true
for the Drives, Files, File, Folders, Folder, and TextStream
objects In the example for the Drive object, you see how to get it from the
FileSystemObject and then use it to output information about the C:
Unlike the Dictionary and FileSystemObject, the Drive object has no
methods You can, however, use some of its properties to change the
characteristics of drives on your system (specifically the Volume label).
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com
Trang 2FileSystem
This returns a string that indicates the file system of a drive
The types that can be returned are FAT, NTFS, or CDFS.
is a local drive, it returns "".
Make sure to use the IsReady() method in your JScript code
before you attempt to access the properties of a drive The current
version of JScript suffers from a lack of good error handling
capabilities, so if an error occurs when you attempt to access a
drive, your JScript code just bombs out with an error Support for
enhanced error handling will be available in an upcoming JScript
version release from Microsoft.
The Drive object enables you to retrieve all the details for a drive Here’s an
Trang 3// about a local or network drive.
Trang 4
The drive.js script uses a FileSystemObject to get access to the
drives property, which is the Drives collection The Drives collection
can be indexed using a string much like the Dictionary You use this
capability to get a Drive object that corresponds to the argument that the
user passed on the command line Next, you output all the information about
the Drive by echoing its properties to the screen You can see the results of
running the drive.js script in Figure 3.5.
The File object represents a file stored on a local or network disk Using a
File object, you can retrieve all the properties of a file, you can open it to
read or write as text, and you can copy, move, or delete the file You can get
an instance of a File object from either a FileSystemObject or Folder
4—System; 8—Disk drive Volume label; 16—
Directory; 32—Archive; 64—Alias (like a link or shortcut); and 128—Compressed.
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com
Trang 5at the destination, it is not overwritten unless
is read-only unless the force flag is specified
a file already exists at the destination, an error
Trang 6ForReading—opens the file for reading only;
ForWriting—opens a file for writing;
ForAppending—opens a file and enables you
to write to the end of the file The format flag can be one of the following:
TristateUseDefault—indicates that the file should be opened using the system default;
TristateTrue—indicates that the file should
be opened as Unicode; TristateFalse—
indicates that the file should be opened as ASCII.
The File object enables you to retrieve all the details for a file; here's an
Trang 7WScript.echo("File " + args(0) + " details:");
The first part of the example displays all of the information that we can
gather from the properties of a file object You’ll notice that we use a custom
function, ShowFileAttributes(), to decode and display the information
about the file attributes The body of ShowfileAttributes() is listed
Trang 8This very simple script displays all the available information about a file,
including its size, name, attributes, and more First you instantiate a
FileSystemObject; you use it to call the
FileSystemObject.getFile() method, which returns a File object
Then you simply echo all the properties of the File object to the screen
Notice that, for simplicity’s sake, you’ve split out the code that checks the file
attributes into a separate function called ShowFileAttributes() The
results of the script are illustrated in Figure 3.6.
The Folder object represents a folder (or directory) on a local or network
disk Using a Folder object, you can retrieve all the properties of a folder,
you can retrieve a collection of the files or folders within it, and you can copy,
move, or delete the folder You can get an instance of a Folder object from
either a FileSystemObject or Drive object.
Trang 9Attributes
This enables you to set or get the attributes of a
folder It is a numeric value that uses a logical combination of the following values: 0—Normal; 1—
ReadOnly; 2—Hidden; 4—System; 8—Disk drive Volume label; 16—Directory; 32—Archive;
64—Alias (like a link or shortcut); and 128—
Trang 12In this sample script, first you instantiate a FileSystemObject, and then
you get a Folder object by calling the
FileSystemObject.getFolder() method At that point, you simply echo
folder properties to the screen The output of the folder.js script is
Earlier in this chapter, you learned about the various objects that you can
access through the FileSystemObject For each object, there is a
collection object that holds a group of objects For example, many Drive
objects can be in a Drives collection These collections are used simply to
represent a group of file system components If you want to find out how to
access these collections, review the FileSystemObject and its methods
All the collections have the same basic capabilities; they can return a count
of entries and enable the user to access individual entries Consequently,
you’re not going to look at each one separately because they are so similar
There is one exception to the rule; the Folders collection has a method that
enables you to add a folder whereas the Drives and Files collections do
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com
Trang 13not In any case, after you learn how to use one collection, it is easy to use
any of the others.
Trang 15For this example, you use the various collection objects to display
information about a disk drive First, you instantiate a FileSystemObject
so that you can access the Drives collection Next, you get the specific
drive that you want by using the argument as the index into the Drives
collection, and then you check the state of the drive using the isReady
property It’s important to check the isReady property; if you don’t check it,
your script might experience errors if the drive is, for example, a CD-ROM
drive that doesn’t currently contain a CD-ROM disk You get the root folder of
the drive using the rootFolder property and display all the subfolders of
the rootFolder by iterating through the subFolders property Finally, you
display all the files in the root folder of the drive by iterating through the
rootFolder.files collection Figure 3.8 shows the output from the
The TextStream object makes it easy to perform sequential access
functions with a text file You can use it to read or skip through a file, to write
to a file, and to track your position within a file Like many of the other
objects, you can’t instantiate it directly Instead, you have to use either a
FileSystemObject or a File object to create a TextStream.
Trang 16TextStream Object Properties
This indicates whether the file pointer is positioned
immediately before the end-of-line marker.
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com
Trang 17WriteLine(string)
This writes a string to an open text file and then
Here’s an example that uses the FileSystemObject to instantiate a
TextStream object that’s used to output a file to the console with line
Trang 18This example uses an instance of the FileSystemObject to create a
TextStream object by calling FileSystemObject.openTextFile()
The openTextFile() method returns a new TextStream object, and then
a loop reads lines from the TextStream until you reach the end of it, at
which point the TextStream.atEndOfStream property returns True
Figure 3.9 shows the output of the textstrm.js script.
instantiate a FileSystemObject , Drives , Drive , Folders , and then Folder
Using VBScript, it’s possible to dynamically resize an array by using the
of an array that I dynamically resize?
Trang 19However, it’s very inefficient because every element in the array is affected by
ReDim The Dictionary object is implemented differently; consequently, it’s more efficient to use when you need a dynamically resizable array of elements.
2. Use the FileSystemObject to write a script that takes a filename and echoes it to
Write a script that takes a folder name as input and writes a list of the files in the folder
to a nicely formatted text file For an advanced exercise, modify the program to go through the subfolders of the folder while outputting file information to a text file.
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com
Trang 20Day 4: The WSH Object Model
The Windows Scripting Host scripting environment provides basically two sets of
objects: objects that are included with the scripting engine and WSH-specific objects You learned about the scripting objects on Day 3, "The Scripting Object Model," and in this chapter you'll learn about the WSH scripting objects.
This chapter assumes basic familiarity with Microsoft OLE and ActiveX OLE
is the standard for embedding objects inside of applications; you may have
used it to put a a spreadsheet excerpt inside a document ActiveX (or the
Component Object Model [COM]) is essentially the object standard that
underlines OLE—it provides the fundamental capabilities to instantiate and
use objects If you'd like more information, refer to the Microsoft web site
Like the scripting objects, the WSH objects provide very powerful tools that enable you
to access and change the Windows environment For example, they enable you to
change network drive mappings and read or modify the Windows registry In this
chapter, you'll cover all the WSH scripting objects; you'll see reference information
describing their properties and methods and examples using each object Like Day 3, this chapter is intended to give you a basic familiarity with the objects In subsequent
chapters, you'll develop examples that use WSH objects combined with standard
The WSH objects are provided by the Windows Scripting Host environment
They are extremely powerful and provide you with the capabilities that you
need to perform many standard tasks The standard objects include
Wscript, WshArguments, WshShell, WshNetwork, WshShortcut,
WshUrlShortcut, WshCollection, WshEnvironment, and
WshSpecialFolders The WSH objects are depicted in Figure 4.1.
Trang 21The name of the object corresponds to the functionality provided by the
object For example, the WshShell object enables you to manipulate the
Windows shell The WshNetwork object enables you to change connections
to the network The purposes of the others should be fairly self-explanatory
given the naming convention Later you’ll find more specifics about each of
the objects, and you’ll see code examples that illustrate the use of each
object Bear in mind that later in the book you’ll have more complete
examples that show you how you can use groups of these objects together
to perform really useful tasks.
The root WSH object is the Wscript object It’s always available when a
script is running in the scripting host, so you don’t have to create an instance
of the object It’s not really a root object from an inheritance perspective, but
it’s easy to think of it as such because it plays a central role for WSH scripts
The Wscript object provides the capability to retrieve information about a
the script’s runtime environment and arguments It also provides the
capability to create objects or retrieve them from files Finally, it provides the
capability to echo information to the screen and to quit with an exit code You
will almost certainly use the Wscript object, if only to create objects or echo
information to the screen!
When you access the object, you use the object name Wscript Unlike the
other objects that are described in this chapter, you don’t have to initialize a
new object instance for it The Wscript or cscript program takes care of
those details for you.
Trang 22up event handling for the object that you create You must specify the PROGID of the object to create it, such as Excel.Application, and you can optionally specify the prefix
of event-handling methods that you implement.
CreateObject() or GetObject() methods.
Trang 23Echo(arg1, arg2, )
This enables you to echo information to the screen If Wscript is executing the script, the output pops up in a dialog box If cscript is executing the script, the output is echoed to the screen, such
as the DOS ECHO batch file command.
The Wscript object is a very powerful object that you use all the time in
your WSH scripts Here’s an example using WSH that shows how you can
create and manipulate ActiveX objects exposed by Microsoft Excel to
programmatically create a spreadsheet:
Trang 24In this script, you’re using the ever present Wscript object to create an
instance of the Excel.Application object Most people recognize that
Excel is the popular spreadsheet provided by Microsoft as part of their Office
Suite However, many people are unaware that Excel provides a very
powerful OLE automation interface that enables you to develop external
programs that control Excel Basically, any external program that can use
OLE automation objects can use Excel programmatically You can do it from
Visual Basic, Delphi, or C++ In your example, you’re controlling it from a
WSH script implemented using the VBScript language You could also do it
using the JScript language in a WSH script; any WSH scripting engine
should be able to access OLE automation objects such as the objects
You begin with the Excel.Application object, and then you call a few
methods and change some Excel properties For more information about the
Excel object model, refer to the programming information provided by
Microsoft The results of running the script using cscript Wscript.vbs
from the command line are illustrated in Figure 4.2.
The WshArguments object provides you with a collection of the arguments
for the current script You can use arguments to customize the execution of
your script at runtime For example, you might define a script that runs a
special backup routine For the backup script, you might want to define
source and destination device arguments That’s one simple example; when
you are creating your script, keep in mind the portions of your script for which
you want to use arguments That helps you to create scripts that you can
write once and reuse over and over without any modification.
To access the WshArguments object, you don’t need to create an instance
using CreateObject() It’s immediately available to a running script from
the Wscript.Arguments property.
Trang 25is also the default property for the WshArguments object.
In the preceding showparams.vbs script, you begin by setting the variable
args to the Wscript.Arguments property The Wscript.Arguments
property returns a WshArguments object Next, you loop through the
Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com