All pastes #253225 Raw Edit

Parse network logs

public text v1 · immutable
#253225 ·published 2006-11-20 15:07 UTC
rendered paste body
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

Function WriteToFile(fileName, text, fso)
	wscript.echo fileName
	
	Set f	= fso.OpenTextFile(fileName & ".log", ForAppending, True)
	
	wscript.echo text
	
	f.WriteLine text
	f.Close
End Function

dim args
set args = wscript.Arguments
if args.Count < 1 then
	wscript.echo "Usage: parse logs.vbs <log file> [output folder]"
	wscript.exit
end if

dim logFile
dim outFolder
logFile = args.Item(0)

dim fso, fin
set fso = CreateObject("Scripting.FileSystemObject")
set fin = fso.OpenTextFile(logFile, ForReading, False)

if (args.Count > 1) then
	outFolder = args.Item(1)
	if fso.FolderExists(outFolder) = False then
		fso.CreateFolder(outFolder)
	end if
else
	outFolder = "."
end if

dim line
dim lastFile : lastFile = "NULL"
dim regex, match, matches
set regex = new RegExp

regex.Pattern = "^\[\d{2}:\d{2}:\d{2} (\w+)].*"
regex.IgnoreCase = True
regex.Global = True

while fin.AtEndOfStream <> True
  line = fin.ReadLine()
	wscript.echo "Parsing line: " & line
	set matches = regex.Execute(line)
	if matches.Count > 0 then
		lastFile = matches.Item(0).Submatches(0) 'get the new file name
	end if
	
	WriteToFile outFolder & "\" & lastFile, line, fso
wend

fin.Close

set fin = Nothing
set fs = Nothing