In this chapter, you will copy and modify the archive management script developed in Chapter 20, "Maintaining a 30-Day Summary Log Archive," in order to develop a new script that maintains a three-month archive of reports on the Windows 2000 Professional workstation located in the Operations Command Center. This new script will then be executed on a monthly basis by the scheduling script developed in Chapter 23, "Collecting Remote Summary Reports."
The last script developed by Molly as part of this project is the archive management script. It will be executed on a scheduled basis on the first day of each month. Its job is to maintain three separate three-month archives on the Windows 2000 Professional workstation, as outlined in the following list.
Figure 25.1 shows the contents of the d:Order_Inventory folders on the Windows 2000 Professional workstation. The LogFiles folder is used to temporarily store the summary report files copied over by the report retrieval script. The report consolidation script then reads the summary reports located in the LogFiles folder in order to create the consolidated reports, which are stored in the ConsolidatedRpts folder. When the archive management script executes, it copies the files stored in the LogFiles folder to either the Sr1_SummaryRpts or Sr2_SummaryRpts folders, as appropriate. Finally, the archive management script deletes any reports stored in the ConsolidatedRpts, Sr1_SummaryRpts, or Sr2_SummaryRpts folders that are more than three months old.
Figure 25.1: Examining the folder structure used to manage and store reports on the Windows 2000 Professional workstation
In order to perform archive management tasks, Molly will need to work with the FileSystemObject object's DeleteFile() method again. The syntax for this method is provided in Chapter 20. In addition, she will need to learn how to work with a number of other methods belonging to the FileSystemObject. These methods include:
In developing her script, Molly will need to determine whether certain folders exist before attempting to access their contents or store files in them. To perform this test, she plans to use the FileSystemObject object's FolderExists() method, which returns a Boolean value of True or False based on the existence of the specified folder. The syntax of the FolderExists() method is shown below.
ObjectReference.FolderExists(FolderName)
ObjectReference is the variable representing an instance of the FileSystemObject object. FolderName specifies the complete path and name of the folder whose existence is to be tested.
The following VBScript statements demonstrate how to determine whether the d:Order_InventoryLogFiles folder exists.
Dim FsoObj Set FsoObj = CreateObject("Scripting.FileSystemObject") If (FsoObj.FolderExists(d:Order_InventoryLogFiles) = False) Then MsgBox "The specified folder does not exist." Else MsgBox "The specified folder exists." End If
Based on the results of the test, a script might perform any number of actions, including:
The first time the archive management script runs, the Sr1_SummaryRpts and Sr2_SummaryRpts folders will not exist. Rather than manually creating these two folders as she has done for other folders used by her scripts, Molly has decided to let the archive management script perform this task. In order to automate this task, Molly will need to use the FileSystemObject object's CreateFolder() method. The syntax for this method is outlined below.
ObjectReference.CreateFolder(FolderName)
ObjectReference is the variable representing an instance of the FileSystemObject object. FolderName specifies the name of the folder to be created.
Note |
Always use the FileSystemObject object's FolderExists() method to determine that a folder does not already exist before attempting to create it. Otherwise, an error will occur. |
Creating a folder is a straightforward task. First you must instantiate the FileSystemObject. Then you can check to make sure that the folder does not already exist using the FolderExists() method before finally using the CreateFolder() method, as demonstrated in the following example.
Dim FsoObj, strWorkingFolder Set FsoObj = CreateObject("Scripting.FileSystemObject") If (FsoObj.FolderExists("d:Order_Inventory Sr1_SummaryRpts ") = false) Then Set strWorkingFolder = _ FsoObj.CreateFolder("d:Order_Inventory Sr1_SummaryRpts ") End If
In this example, the script creates a folder called Sr1_SummaryRpts in the D:Order_Inventory folder if the Sr1_SummaryRpts folder does not already exist.
Tip |
You cannot use the CreateFolder() method to reinitialize an existing folder. If you attempt to do so, your script will receive an error. However, you can use the DeleteFolder() method to delete the folder and then recreate it again using the CreateFolder() method. |
The archive management script will need to be able to move files from the Log Files staging folder to the Sr1_SummaryRpts and Sr2_SummaryRpts archive folders. To perform this task, Molly will need to use the FileSystemObject object's MoveFile() method. This method has the following syntax:
ObjectReference.MoveFile (Source, Target )
ObjectReference is a variable representing an instance of the FileSystemObject object. Source specifies the location of the file or files to be moved, and Target specifies the destination folder where the file or files are to be stored.
Tip |
As an alternative to the FileSystemObject object's MoveFile() method, you could use the File object Move() method. However, this method only processes one file at a time, so in order to use it, you would have to set up a loop to process all of the files in the LogFiles folder. |
The following VBScript statements demonstrate how to use the DeleteFile() method to move all the text files located in the d:Order_InventoryLog Files folder to the d:Order_InventorySr1_SummaryRpts folder.
Dim FsoObj Set FsoObj = CreateObject("Scripting.FileSystemObject") Set FsoObj = CreateObject("Scripting.FileSystemObject") FsoObj.MoveFile "d:Order_Inventory LogFiles*.txt", _ "d:Order_InventorySr1_SummaryRpts "
Molly intends to copy and modify the archive management script that was presented in Chapter 20 when developing the archive management script for the Windows 2000 Professional workstation. In addition to the functionality already provided by that script, Molly intends to enable the script to support the following operations:
The Initialization Section, shown below, begins with the Option Explicit statement in order to enforce script variable naming throughout the script. Next, it defines variables that are used globally. Then it defines a constant that specifies a text string to be used by all pop-up dialog boxes displayed by the script when run in debug mode. Finally, it instantiates the FileSystemObject and WshShell objects.
Option Explicit Dim strVerifyExecutionSchedule, strDeleteMonth, strEventLog, strDebug Dim strSvrList, strArchive, strSvr1Folder, strSvr2Folder, strConsolFolder Dim FsoObj, WshShl Const cTitleBarMsg = "Master Archive Management Script" Set WshShl = WScript.CreateObject("WScript.Shell") Set FsoObj = CreateObject("Scripting.FileSystemObject")
The Main Processing Section, shown below, consists of a collection of subroutines and function calls. It begins by executing the OkToRunToday() function, which returns a value of True if the script is being executed on the first day of the month. The value returned by this function is assigned to the strVerify ExecutionSchedule variable, which is then tested to determine whether or not the script may execute. If its value is not set equal to True, a message is written to the Windows application event log and the TerminateScript() subroutine is run in order to halt the script's execution.
strVerifyExecutionSchedule = OkToRunToday() If strVerifyExecutionSchedule = "True" Then SetDefaultSettings() GetRegistrySettings() If strEventLog = "Enabled" Then WriteToEventLog("Consolidated Summary Report Archive Manager executing.") End If MoveSummaryReports() MonthToDelete() RemoveOldReportFiles() If strEventLog = "Enabled" Then WriteToEventLog("Consolidated Summary Report Archive Manager finished.") End If Else WriteToEventLog("Consolidated Summary Report Archive Manager execution" & _ " terminated - invalid execution schedule.") TerminateScript() End If TerminateScript()
If the value assigned to the strVerifyExecutionSchedule variable is set equal to True, then the SetDefaultSettings() subroutine is called in order to establish default configuration settings. Next GetRegistrySettings() is executed. This subroutine extracts configuration settings stored in the Windows registry, overriding matching default configuration settings. If event logging is enabled, a message is then written to the Windows application event log specifying that the script is now running. Next the MoveSummaryReports() subroutine is executed. It copies summary reports from a staging folder to one of two archive folders for long-term storage. Then the MonthToDelete() subroutine runs and figures out which month's worth of summary and consolidated reports should be deleted. This information is then used by the RemoveOldReportFiles() subroutine, which performs the actual deletion of report files. Finally, another message is written to the Windows application event log if event logging is enabled and the script's execution is halted by calling the TerminateScript() subroutine.
The OkToRunToday() subroutine, shown below, uses the VBScript Day() and Date() functions to determine if the script is being executed on the first day of the month. If it is, then the value of a variable named OkToRunToday is set equal to True.
Function OkToRunToday() If Day(Date()) = 1 Then OkToRunToday = "True" End If If strDebug = "Enabled" Then MsgBox "OkToRunToday = " & OkToRunToday End If End Function
If debugging is enabled, the value of OkToRunToday is displayed using the VBScript MsgBox() function.
The SetDefaultSettings() subroutine, shown below, sets default configuration settings for the script so that it can continue to execute in the event that it experiences a problem retrieving its configuration settings from its associated registry values. If debugging is enabled, the value of each variable modified by this subroutine is displayed in a pop-up dialog box.
Sub SetDefaultSettings() strEventLog = "Enabled" strDebug = "Disabled" strSvrList = "SERV0001 SERV0002" strArchive = "d:Order_InventroryLogFiles" strSvr1Folder = "d:Order_InventorySr1_SummaryRpts" strSvr2Folder = "d:Order_InventorySr2_SummaryRpts" strConsolFolder = "d:Order_InventoryConsolidatedRpts" If strDebug = "Enabled" Then MsgBox "Default settings initialized: " & vbCrLf & vbCrLf & _ "strEventLog" & vbTab & "=" & vbTab & strEventLog & vbCrLf & _ "strDebug" & vbTab & vbTab & "=" & vbTab & strDebug & vbCrLf & _ "strSvrList" & vbTab & vbTab & "=" & vbTab & strSvrList & vbCrLf & _ "strArchive" & vbTab & "=" & vbTab & strArchive & vbCrLf & _ "strSvr1Folder " & vbTab & "=" & vbTab & strSvr1Folder & vbCrLf & _ "strSvr2Folder" & vbTab & "=" & vbTab & strSvr2Folder & vbCrLf & _ "strConsolFolder" & vbTab & "=" & vbTab & strConsolFolder, , cTitleBarMsg End If End Sub
The GetRegistrySettings() subroutine, shown below, begins by specifying the On Error Resume Next statement. This will ensure that this script's execution is not halted in the event of a problem retrieving registry values. As each registry value is read using the WshShell object's RegRead() method, the value of the Err object's default property (Err.Number) is checked to determine whether an error has occurred. If an error has occurred, a message is written to the Windows application event log and the value of Err.Number is reset to zero.
Sub GetRegistrySettings() On Error Resume Next strEventLog = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsEventLogging") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strEventLog.") Err.Number = 0 End If End If strDebug = WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsDebugMode") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strDebug.") Err.Number = 0 End If End If strSvrList = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsWin2000Svrs") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strSvrList.") Err.Number = 0 End If End If strArchive = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsRptArchive") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strArchive.") Err.Number = 0 End If End If strSvr1Folder = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsSvr1Folder") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strSvr1Folder.") Err.Number = 0 End If End If strSvr2Folder = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsSvr2Folder") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strSvr2Folder.") Err.Number = 0 End If End If strConsolFolder = _ "WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsConsolFolder") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strConsolFolder.") Err.Number = 0 End If End If If strDebug = "Enabled" Then MsgBox "Registry settings retrieved: " & vbCrLf & vbCrLf & _ "strEventLog" & vbTab & "=" & vbTab & strEventLog & vbCrLf & _ "strDebug" & vbTab & vbTab & "=" & vbTab & strDebug & vbCrLf & _ "strSvrList" & vbTab & vbTab & "=" & vbTab & strSvrList & vbCrLf & _ "strArchive" & vbTab & "=" & vbTab & strArchive & vbCrLf & _ "strSvr1Folder " & vbTab & "=" & vbTab & strSvr1Folder & vbCrLf & _ "strSvr2Folder" & vbTab & "=" & vbTab & strSvr2Folder & vbCrLf & _ "strConsolFolder" & vbTab & "=" & vbTab & strConsolFolder, , cTitleBarMsg End If End Sub
The MoveSummaryReports() subroutine, shown below, is responsible for moving the summary reports collected from the Windows 2000 servers (stored in the LogFiles staging folder on the Windows 2000 Professional workstation) to the Sr1_SummaryRpts and Sr2_SummaryRpts archive folders. It begins by specifying a localized instance of the On Error Resume Next statement in order to prevent an error during one of the script's two move operations from terminating the script's execution. If debugging is enabled, a pop-up dialog box will display the name of each summary report as it is being processed.
Sub MoveSummaryReports() On Error Resume Next Dim strNewFolder1, strNewFolder2 If strDebug = "Enabled" Then MsgBox "Moving......." & vbCrLf & vbCrLf & _ strArchive & "" & Left(strSvrList, 8) & "*.*" & vbCrLf & _ strArchive & "" & Right(strSvrList, 8) & "*.*" End If If (FsoObj.FolderExists(strArchive) = False) Then TerminateScript() Else If (FsoObj.FolderExists(strSvr1Folder) = False) Then Set strNewFolder1 = FsoObj.CreateFolder(strSvr1Folder) End If If (FsoObj.FolderExists(strSvr2Folder) = False) Then Set strNewFolder2 = FsoObj.CreateFolder(strSvr2Folder) End If FsoObj.MoveFile strArchive & "" & Left(strSvrList, 8) & "*.*", _ strSvr1Folder FsoObj.MoveFile strArchive & "" & Right(strSvrList, 8) & "*.*", _ strSvr2Folder End If End Sub
Next the FileSystemObject object's FolderExists() method is used to verify that the LogFiles staging folder is accessible. If it is not, the TerminateScript() subroutine is called to halt the script's execution. Otherwise, the script uses the FolderCreate() method to create the Sr1_SummaryRpts and Sr2_SummaryRpts archive folders if they do not exist (for example, if the script is running for the first time).
Finally, the FileSystemObject object's MoveFile() method is used to move the summary reports to the appropriate archive folder. The Left() and Right() functions are used to parse out the server name embedded in the strSvrList variable so that the subroutine will know to which archive folder to move the summary reports.
The MonthToDelete() subroutine, shown below, is responsible for determining which month's worth of summary and consolidated report files is to be deleted. It begins by defining three variables. The intGetSlashPosition variable is used to store a value indicating the location of the first backslash (/) character in the current date. The strCurrentMonth variable will be used to store a numeric value indicating the current month.
The value assigned to intGetSlashPosition is determined by using the Instr() function to search for the backslash (/) character in the date as retrieved by the Date() function. The value of strCurrentMonth is then determined using the Mid() function to parse out the month portion of the date (which is in the format of mm/dd/yyyy). The month value is parsed out by taking all the characters from the first character position until the occurrence of the first backslash (/) character (expressed as intGetSlashPosition – 1).
The value of strDeleteMonth is then determined by subtracting 4 from strCurrentMonth. If, for example, the current date is June 1, then the value of strDeleteMonth will be 1 (5 – 4 = 1). Four If…Then statements are then set up to adjust the value of strDeleteMonth in the event that the current month is either January, February, March, or April. For example, if the current month is April, then 4 minus 4 will equal zero. Since the month that should be deleted in this instance is December, the first If statement checks to see if the value assigned to strDeleteMonth is 0 and changes its value to 12 if it is. Likewise, similar adjustments are made for the first three months of the year.
Sub MonthToDelete() Dim intGetSlashPosition, strCurrentMonth intGetSlashPosition = Instr(Date(), "/") strCurrentMonth = Mid(Date(), 1, intGetSlashPosition - 1) strDeleteMonth = strCurrentMonth - 4 If strDeleteMonth = 0 Then strDeleteMonth = "12" End If If strDeleteMonth = -1 Then strDeleteMonth = "11" End If If strDeleteMonth = -2 Then strDeleteMonth = "10" End If If strDeleteMonth = -3 Then strDeleteMonth = "9" End If If strDebug = "Enabled" Then MsgBox "strDeleteMonth = " & strDeleteMonth End If End Sub
The RemoveOldReportFiles() subroutine, shown below, is responsible for deleting summary and consolidated reports more than three months old. It begins with the On Error Resume Next statement in order to prevent any errors that occur when deleting the files from halting the script's execution.
If debug mode is enabled, a pop-up dialog box is displayed, showing the string that the subroutine will use to identify which summary and consolidated reports it will delete. Next, the DeleteFile() method is used to delete the files. As you can see, the string that specifies which files are to be deleted is somewhat involved. It is assembled by specifying the name of the archive folder where the reports are stored (the Sr1_SummaryRpts or Sr2_SummaryRpts folders) and then appending the backslash () character, followed by the name of the files to be deleted.
The name indicating which files are to be deleted is established by performing the following steps:
Sub RemoveOldReportFiles() On Error Resume Next Dim strSummaryRptPath If strDebug = "Enabled" Then MsgBox "Deleting ......" & vbCrLf & vbCrLf & _ strSvr1Folder & "" & Left(strSvrList, 8) & "_" & strDeleteMonth & _ "*_SumRpt.txt" & vbCrLf & _ strSvr2Folder & "" & Right(strSvrList, 8) & "_" & strDeleteMonth & _ "*_SumRpt.txt" & vbCrLf & _ strConsolFolder & "" & strDeleteMonth & "*_ConsolSumRpt.txt" End If strSummaryRptPath = "d:Order_InventorySummaryRpts" FsoObj.DeleteFile strSvr1Folder & "" & Left(strSvrList, 8) & "_" & _ strDeleteMonth & "*_SumRpt.txt" FsoObj.DeleteFile strSvr2Folder & "" & Right(strSvrList, 8) & "_" & _ strDeleteMonth & "*_SumRpt.txt" FsoObj.DeleteFile strConsolFolder & "" & strDeleteMonth & _ "*_ConsolSumRpt.txt" End Sub
The WriteToEventLog() subroutine, shown below, uses the WshShell object's LogEvent() method to write an informational message, passed to it as an argument, to the Windows application's event log.
Sub WriteToEventLog(strMessage) WshShl.LogEvent 4, strMessage End Sub
The TerminateScript() subroutine, shown below, halts the script's execution using the WScript object's Quit() method.
Sub TerminateScript() WScript.Quit() End Sub
The fully assembled archive management script is shown below. It will be executed on the first day of each month and will maintain a three-month archive of summary reports collected from both Windows 2000 servers, as well as a three-month archive of consolidated summary reports.
'************************************************************************* 'Script Name: Script 25.1.vbs 'Author: Jerry Ford 'Created: 04/13/03 'Description: This script maintains a 90-day log archive of both summary 'and consolidated Order_Inventory reports '************************************************************************* 'Initialization Section Option Explicit Dim strVerifyExecutionSchedule, strDeleteMonth, strEventLog, strDebug Dim strSvrList, strArchive, strSvr1Folder, strSvr2Folder, strConsolFolder Dim FsoObj, WshShl Const cTitleBarMsg = "Master Archive Management Script" Set WshShl = WScript.CreateObject("WScript.Shell") Set FsoObj = CreateObject("Scripting.FileSystemObject") 'Main Processing Section strVerifyExecutionSchedule = OkToRunToday() If strVerifyExecutionSchedule = "True" Then SetDefaultSettings() GetRegistrySettings() If strEventLog = "Enabled" Then WriteToEventLog("Consolidated Summary Report Archive Manager executing.") End If MoveSummaryReports() MonthToDelete() RemoveOldReportFiles() If strEventLog = "Enabled" Then WriteToEventLog("Consolidated Summary Report Archive Manager finished.") End If Else WriteToEventLog("Consolidated Summary Report Archive Manager execution" & _ " terminated - invalid execution schedule.") TerminateScript() End If TerminateScript() 'Procedure Section Function OkToRunToday() If Day(Date()) = 1 Then OkToRunToday = "True" End If If strDebug = "Enabled" Then MsgBox "OkToRunToday = " & OkToRunToday End If End Function Sub SetDefaultSettings() strEventLog = "Enabled" strDebug = "Disabled" strSvrList = "SERV0001 SERV0002" strArchive = "d:Order_InventroryLogFiles" strSvr1Folder = "d:Order_InventorySr1_SummaryRpts" strSvr2Folder = "d:Order_InventorySr2_SummaryRpts" strConsolFolder = "d:Order_InventoryConsolidatedRpts" If strDebug = "Enabled" Then MsgBox "Default settings initialized: " & vbCrLf & vbCrLf & _ "strEventLog" & vbTab & "=" & vbTab & strEventLog & vbCrLf & _ "strDebug" & vbTab & vbTab & "=" & vbTab & strDebug & vbCrLf & _ "strSvrList" & vbTab & vbTab & "=" & vbTab & strSvrList & vbCrLf & _ "strArchive" & vbTab & "=" & vbTab & strArchive & vbCrLf & _ "strSvr1Folder " & vbTab & "=" & vbTab & strSvr1Folder & vbCrLf & _ "strSvr2Folder" & vbTab & "=" & vbTab & strSvr2Folder & vbCrLf & _ "strConsolFolder" & vbTab & "=" & vbTab & strConsolFolder, , cTitleBarMsg End If End Sub Sub GetRegistrySettings() On Error Resume Next strEventLog = _ "WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsEventLogging") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strEventLog.") Err.Number = 0 End If End If strDebug = WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsDebugMode") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strDebug.") Err.Number = 0 End If End If strSvrList = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsWin2000Svrs") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strSvrList.") Err.Number = 0 End If End If strArchive = _ "WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsRptArchive") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strArchive.") Err.Number = 0 End If End If strSvr1Folder = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsSvr1Folder") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strSvr1Folder.") Err.Number = 0 End If End If strSvr2Folder = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsSvr2Folder") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strSvr2Folder.") Err.Number = 0 End If End If strConsolFolder = _ WshShl.RegRead("HKLMSoftwareIntuitVBScriptsMstSumRptsConsolFolder") If Err <> 0 Then If strEventLog = "Enabled" Then WriteToEventLog ("Summary Report Collection script - Using " & _ "default for strConsolFolder.") Err.Number = 0 End If End If If strDebug = "Enabled" Then MsgBox "Registry settings retrieved: " & vbCrLf & vbCrLf & _ "strEventLog" & vbTab & "=" & vbTab & strEventLog & vbCrLf & _ "strDebug" & vbTab & vbTab & "=" & vbTab & strDebug & vbCrLf & _ "strSvrList" & vbTab & vbTab & "=" & vbTab & strSvrList & vbCrLf & _ "strArchive" & vbTab & "=" & vbTab & strArchive & vbCrLf & _ "strSvr1Folder " & vbTab & "=" & vbTab & strSvr1Folder & vbCrLf & _ "strSvr2Folder" & vbTab & "=" & vbTab & strSvr2Folder & vbCrLf & _ "strConsolFolder" & vbTab & "=" & vbTab & strConsolFolder, , cTitleBarMsg End If End Sub Sub MoveSummaryReports() On Error Resume Next Dim strNewFolder1, strNewFolder2 If strDebug = "Enabled" Then MsgBox "Moving......." & vbCrLf & vbCrLf & _ strArchive & "" & Left(strSvrList, 8) & "*.*" & vbCrLf & _ strArchive & "" & Right(strSvrList, 8) & "*.*" End If If (FsoObj.FolderExists(strArchive) = False) Then TerminateScript() Else If (FsoObj.FolderExists(strSvr1Folder) = False) Then Set strNewFolder1 = FsoObj.CreateFolder(strSvr1Folder) End If If (FsoObj.FolderExists(strSvr2Folder) = False) Then Set strNewFolder2 = FsoObj.CreateFolder(strSvr2Folder) End If FsoObj.MoveFile strArchive & "" & Left(strSvrList, 8) & "*.*", _ strSvr1Folder FsoObj.MoveFile strArchive & "" & Right(strSvrList, 8) & "*.*", _ strSvr2Folder End If End Sub Sub MonthToDelete() Dim intGetSlashPosition, strCurrentMonth intGetSlashPosition = Instr(Date(), "/") strCurrentMonth = Mid(Date(), 1, intGetSlashPosition - 1) strDeleteMonth = strCurrentMonth - 4 If strDeleteMonth = 0 Then strDeleteMonth = "12" End If If strDeleteMonth = -1 Then strDeleteMonth = "11" End If If strDeleteMonth = -2 Then strDeleteMonth = "10" End If If strDeleteMonth = -3 Then strDeleteMonth = "9" End If If strDebug = "Enabled" Then MsgBox "strDeleteMonth = " & strDeleteMonth End If End Sub Sub RemoveOldReportFiles() On Error Resume Next Dim strSummaryRptPath If strDebug = "Enabled" Then MsgBox "Deleting ......" & vbCrLf & vbCrLf & _ strSvr1Folder & "" & Left(strSvrList, 8) & "_" & strDeleteMonth & _ "*_SumRpt.txt" & vbCrLf & _ strSvr2Folder & "" & Right(strSvrList, 8) & "_" & strDeleteMonth & _ "*_SumRpt.txt" & vbCrLf & _ strConsolFolder & "" & strDeleteMonth & "*_ConsolSumRpt.txt" End If strSummaryRptPath = "d:Order_InventorySummaryRpts" FsoObj.DeleteFile strSvr1Folder & "" & Left(strSvrList, 8) & "_" & _ strDeleteMonth & "*_SumRpt.txt" FsoObj.DeleteFile strSvr2Folder & "" & Right(strSvrList, 8) & "_" & _ strDeleteMonth & "*_SumRpt.txt" FsoObj.DeleteFile strConsolFolder & "" & strDeleteMonth & _ "*_ConsolSumRpt.txt" End Sub Sub WriteToEventLog(strMessage) WshShl.LogEvent 4, strMessage End Sub Sub TerminateScript() WScript.Quit() End Sub
In this chapter, you learned how to create an archive management script that maintained three separate report archives, each of which stores a minimum of three months' worth of reports. In addition, you observed as Molly added debug logic to the script and adapted it to retrieve its configuration settings from the Windows registry.
Part I - Introducing Microsoft VBScriptBasics
Part II - Professional Project 1 Desktop Administration Using VBScript and the WSH
Part III - Professional Project 2 Analyzing Application Logs
Part IV - Professional Project 3 Creating a Centralized Report Management Station
Part V - Professional Project 4 Reporting Application Summary Data via the Web
Part VI - Introducing Microsoft VBScriptBasics