If you create your own .bks file, remember to append a trailing backslash when
you specify a folder. If you don't, NTCatalog.js will think that the line refers
to a file. For example, if you intend to back up the folder C:\ Program Files,
make sure that you type
C:\Program Files in the .bks file.
Introducing NTCatalog.js
NTCatalog.js opens a .bks file and creates a CSV file that contains the names
of all files selected for backup. The script requires the FileDB.wsc component,
so before you can use NTCatalog.js, you need to obtain FileDB.wsc and register
it on your computer. You can download FileDB.wsc from the Windows Scripting
Solutions Web site at http://www.windowsitpro.com/windowsscripting,
InstantDoc ID 93451.
NTCatalog.js requires the CScript host, so you need to start the command line
with the CScript keyword if CScript isn't the default host. To configure CScript
as your default host, type the following command at a command prompt:
cscript //h:cscript //nologo //s
The syntax for NTCatalog.js is
[cscript] NTCatalog.j
s bksfile csvfile [/n]
where bksfile is the name of the .bks file and csvfile is the
name of the CSV file that the script should create. If either filename contains
spaces, put the filename in quotes.
By default, the CSV file will be overwritten if it already exists. If you don't
want to overwrite an existing file and prefer to generate an error message instead,
add the /n option to the command line.
Inside NTCatalog.js
Listing 1, shows an excerpt from
NTCatalog.js. The script begins by defining some global variables, then executes
the main function, which provides most of the script's functionality. The return
value of the main function is the script's exit code.
The main function declares some local variables, then determines whether it
was executed with at least two command-line arguments. If there are fewer than
two arguments, or if the /? argument is present, the main function executes
the usage function, which displays a short usage message and ends the script.
Otherwise, the main function uses the scriptHost function to determine the name
of the script host executing the script. If the script host isn't cscript.exe,
the main function echoes an error message to the screen and ends with a non-zero
exit code.
When the script host checks out, the main function creates an instance of the
FileSystemObject object and uses it to verify the existence of the .bks file.
If the .bks file doesn't exist, or if the /n option was specified on the command
line and the output file already exists, the script ends with a non-zero exit
code.
Next, the code at callout A creates an instance of the FileDB object. The code
then opens the .bks file, as callout B shows. Note that the Open-TextFile method's
fourth parameter is true, indicating that the .bks file is a Unicode
text file.
At this point, the .bks file has been opened as a TextStream object, and the
main function can loop through the file, which it does by using the While statement.
As long as the AtEndOfStream property is false (indicating the end of
file has not been reached), the loop will continue.
Inside the While loop, the main function reads a line of text and uses the
search method at callout C to determine whether the line ends with the string
" /exclude" (including the leading space, but not including the quotes). The
search method's parameter is a regular expression that specifies the pattern
to search for. The search method returns a number indicating the pattern's starting
position inside the string. If the pattern isn't found, the search method returns
the value -1 in the pos variable. (The trailing "i" after the closing "/" in
the regular expression specifies a case-insensitive match.)