Results 1 to 2 of 2
  1. #1
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    FileSystemObject OpenAsTextStream Sample (VBScript)

    If you are faced with the problem that you need to parse an entire month's web server log files for hits on a particular page or folder, you could use a script like this. Note the special locations specified for the files:
    <pre>'Copyright Jefferson Scher Dec. 2, 2002
    'Parses log files in C:TestingLogs

    Dim oFSO, oFiles, oFile, oStream1, oStream2, strMisc

    'set up objects
    Set oFSO = WScript.CreateObject("scripting.filesystemobject")
    Set oFiles = oFSO.Drives("C").RootFolder. _
    SubFolders("Testing").SubFolders("Logs").Files

    'setup output file (creates it if nonexistent)
    Set oStream1 = oFSO.OpenTextFile("C:TestingLogGems.txt", 8, True)

    'cycle through all the .LOG files in the target folder
    'would be nice to give user some feedback on progress, but how???
    For Each oFile In oFiles
    If UCase(Right(oFile.Name, 4)) = ".LOG" Then
    'open log file as text stream
    Set oStream2 = oFile.OpenAsTextStream
    Do
    'read line from .LOG file
    strMisc = oStream2.ReadLine
    'check path and output to loggems.txt if relevant
    If (InStr(1, strMisc, "/MyASPapp/", 1) > 0) Then
    oStream1.WriteLine strMisc
    End If
    'this could be made a condition of the loop...
    If oStream2.AtEndOfStream Then
    Exit Do
    End If
    Loop
    oStream2.Close
    End If
    Next
    'clean up objects
    oStream1.Close
    Set oStream1 = Nothing
    Set oStream2 = Nothing
    Set oFile = Nothing
    Set oFiles = Nothing
    Set oFSO = Nothing
    MsgBox "Done!"</pre>

    In my case I then used Excel 2000's Data|Get External Data|Import Text File... wizard to turn the comma-delimited file into a report. Hope this helps.

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts

    Re: FileSystemObject OpenAsTextStream Sample (VBScript)

    Hi Jefferson,
    <hr>'would be nice to give user some feedback on progress, but how???<hr>
    You could give a status report with something like:
    Dim Counter, Message
    Counter = Counter + 1
    If Counter Mod 100 = 0 Then
    Message = "Have worked so far with " & TheCount &
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •