Parsing Fixed-Width Logs

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Fixed-width text log fields are parsed differently from CSV log fields. In a comma-separated-values log, an individual field can contain any number of characters; by definition, a field contains all the characters that precede a comma. For example, the following are valid comma-separated-values fields, even though the fields consist of different field lengths:

Computer,Domain WebServer,fabrikam.com FinancialAccountingServer,finance.accounting.fabrikam.com 

In a fixed-width text log, fields are delimited by length rather than by a comma or other character. The fields in any given record must be exactly the same size as fields in all the records.

For example, in NetSetup.log, there are three fields: date, time, and description. As shown in the following log excerpt, date begins with the first character, time with the seventh character, and description with the sixteenth character (numbers have been added to help indicate character positions):

123456789012345678901234567890123456789012345678901234567890 08/16 11:50:49 NetpDoDomainJoin 08/16 11:50:49 NetpMachineValidToJoin: 'WINTEST' 08/16 11:50:49 NetpGetLsaPrimaryDomain: status: 0x0 08/16 11:50:49 NetpMachineValidToJoin: status: 0x0 

When this log is parsed, the first record is split into these three fields:

  • 08/16
  • 11:50:49
  • NetpDoDomainJoin

Scripting Steps

Listing 12.18 contains a script that parses the fixed-width-column log Netsetup.log. To carry out this task, the script must perform the following steps:

  1. Create a constant (ForReading) to be used with the FileSystemObject.
  2. Create an instance of the FileSystemObject.
  3. Open the Netsetup log (C:\Windows\Debug\Netsetup.log).
  4. Create a loop for reading each line in the file.
  5. Read the first line, and temporarily store the contents in the variable strLineToParse.
  6. Use the Mid function to parse the Date, Time, and Description from the variable.
    • Date represents the first six characters in the string.
    • Time begins at character position 7 and is nine characters long.
    • Description begins at character position 16 and makes up the remainder of the line.
  7. Echo the values of the individual fields.
  8. Repeat the loop with the next line in the log file.
  9. Close the log file.

Listing 12.18   Parsing a Fixed-Width-Column Log

1 2 3 4 5 6 7 8 9 10 11 12 13 14 
Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile("C:\Windows\Debug\Netsetup.log", _     ForReading) Do While objTextFile.AtEndOfStream <> True     strLinetoParse = objTextFile.ReadLine     dtmEventDate = Mid(strLinetoParse, 1, 6)     dtmEventTime = Mid(strLinetoParse, 7, 9)     strEventDescription = Mid(strLinetoParse, 16)     Wscript.Echo "Date: " & dtmEventDate     Wscript.Echo "Time: " & dtmEventTime     Wscript.Echo "Description: " & strEventDescription & VbCrLf Loop objTextFile.Close


send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net