While thinking about how to develop the archive management script, Molly decided to use the
FileSystemObject
object's
DeleteFile()
method instead of the
File
object's
Delete()
method. Using the
DeleteFile()
method, she'll be able to delete summary
Molly also wants to record a message in the Windows event log when the script runs so that she will have a record of its execution. She'll need to use the WshShell object's LogEvent() method. Additionally, she'll need to learn how to use the built-in VBScript Instr() function when executing the subroutine that determines which month's worth of files are to be deleted.
For a time, Molly thought about creating a custom log file for her applications to which the scripts would continuously append messages as they were executed. She ultimately gave up on this idea as being too much work for too little gain and has instead decided to leverage the availability of the Windows application event log. To do this, she will have to use the WshShell object's LogEvent() method, which has the following syntax.
ObjectReference .LogEvent( intEventType , strMsg [, strComputer ])
ObjectReference
is the variable representing an instance of the
FileSystem Object
.
IntEventType
is a numeric value that specifies the event type. Table 20.2 provides a listing of the available event types.
StrMsg
represents the message to be recorded in the application event log, and
strComputer
is an optional parameter that specifies the
|
Value |
Description |
|---|---|
|
|
Specifies a successful event |
|
1 |
Specifies an error event |
|
2 |
Specifies a warning event |
|
4 |
Specifies an informational event |
|
8 |
Specifies a successful audit event |
|
16 |
Specifies a failed audit event |
| Note |
Windows NT, 2000, and XP all maintain application event logs. On Windows 98 and Me, application events are stored in Wsh.log , which resides in the same folder as the Windows system files (typically C:\Windows ). |
The following example
Set WshShl = WScript.CreateObject("WScript.Shell") WshShl.LogEvent 1, "TestScript.vbs -- Now executing"
The
LogEvent()
method returns a Boolean value of
True
when it successfully
Set WshShl = WScript.CreateObject("WScript.Shell") intAge = GetUserAge() If intAge <= 18 Then WshShl.LogEvent 1, "Error -
User
not authorized to execute script" Else WshShl.LogEvent 0, "Script now executing" End If Function GetUserAge() GetUserAge = InputBox("How old are you?") End Function
In this example, the number entered by the user is checked to determine whether or not the script should execute. If the user is older than 18, the script is permitted to execute,
One of the steps that Molly will need to perform in the archive management scripts is to determine which month's worth of summary reports to delete from the Windows 2000 server where the order\inventory system resides. To perform this task, she will need to leverage a number of different VBScript functions, including the Instr() , Mid() , and Date() functions. The Mid() function was previously introduced in Chapter 17 "Using Configuration Files to Control Script Execution" and the Date() function was introduced in Chapter 19 "Scheduling Script Execution". The Instr() function provides the ability to retrieve a numeric value representing the first occurrence of one string within another. Its syntax is outlined below.
InStr([ StartPosition ,] String1 , String2 [, CompType ])
StartPosition is an option parameter that is used to specify the starting character position within String1 where the search is to begin. String1 represents the expression to be searched. String2 represents the string to search for. CompType is an optional numeric value that specifies the type of comparison. A value of specifies a binary comparison and a value of 1 specifies a text comparison. The value returned by the Instr() method varies based on multiple criteria, as outlined in Table 20.3.
|
Value |
Condition |
|---|---|
|
|
String1 is zero length |
|
Null |
String1 is null |
|
Null |
String2 is null |
|
Start |
String2 is zero length |
|
|
String2 not found in String1 |
|
|
Start is greater than the length of String2 |
|
Position where a match is found |
String2 found within String1 |