Flylib.com

Books Software

 
 
 

Other VBScript Language Elements Needed to Build the Archive Management Script


Other VBScript Language Elements Needed to Build the Archive Management Script

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 reports on a month-by-month basis within a single DeleteFile() operation. Using the Delete() method, Molly would have to create a loop and iterate though a list of files stored in the D:\Order_Inventory\SummaryRpts folder looking for the file to delete.

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.

The WshShell Object's LogEvent() Method

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 name or IP address of another computer where the event should be sent. If omitted, the event is recorded locally.

Table 20.2: Windows Event Types

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 demonstrates how to use the LogEvent() method to write a message to the application event log.

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 writes a message to the Windows application event log and a value of False when it fails to write the message, as demonstrated in the following example.

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, otherwise it is not. A different message is recorded to the application event log based on the user's reported age.

The Built-in VBScript Instr() Function

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.

Table 20.3: Instr() Return Values

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