'script to read lines of event viewer defrag online
'The input file is gather using MS EventCombMT Id 1221
'author: Felipe Ferreira Date:11/07/07 Version:2.0
'dynamic get each file log from the folder
'Ouput only Server SG, size free into same.txt 
'thanks dm_4ever for RegEx help
'line example::
'1221,INFORMATIONAL,MSExchangeIS Mailbox Store,Tue Jul 17 00:30:10 2007,No User,The database "SG2\DB4" has 1058 megabytes of free space after online defragmentation has terminated.      For more information, click http://www.microsoft.com/contentredirect.asp.  

Dim inputfile, outputfile, strText, arrValues, outputfilepath
outputfilepath = "C:\temp\DefragSPACE\"

outputfile = "DefragFreeSpace.txt"
Set iFSO = CreateObject("Scripting.FilesyStemObject")    
Set oFSO = CreateObject("Scripting.FilesyStemObject")    
Set ofile = oFso.OpenTextFile(outputfile,2, True)

Dim fs,f,f1,fc	'oggetto FileSystem 'oggetto folder 	'oggetto file da cancellare 'array di file
Set fs = CreateObject("Scripting.FileSystemObject")

Set f = fs.GetFolder(outputfilepath)
Set fc = f.Files
'Goes thru each File in the folder
For Each f1 in fc
		inputfile=f1.name
		Set ifile = iFSO.OpenTextFile(inputfile)  
'Goes thru each line 
  Do until ifile.AtEndOfLine 
	strText = ifile.ReadLine    
	If Instr(strText, "1221") > 0 Then   ' Found Correct String
		arrValues = Split(strText,",")
		Do While i < UBound(arrValues) 
    		'Wscript.echo "ArrValues(" & i & "): " & arrValues(i)
			i = i + 1
		loop
		'Array(5) contains needed info: The database "SG1\Mailbox Store 1 (1DCMX)" has 7457 megabytes of free space after online defragmentation has terminated.
		strDataP = arrValues(5)
		Dim RegEx : Set RegEx = New RegExp
		RegEx.Pattern = "database\s""(.*)""\shas ([0-9]+)"
		RegEx.IgnoreCase = True
		Dim colMatches : Set colMatches = RegEx.Execute(strDataP)
		'WScript.Echo colMatches(0).SubMatches(0)
		'WScript.Echo colMatches(0).SubMatches(1)

		strFinal = colMatches(0).SubMatches(0) & ", " & colMatches(0).SubMatches(1)
		wscript.echo strFinal
		oFile.WriteLine strFinal
	end if	

  loop 'Thru each Line
 oFile.WriteLine inputfile
oFile.WriteLine "----------------------------------------"
next 'Thru each file in the folder
'The database "SG1\Mailbox Store 1 (DCMX)" has 7457 megabytes

wscript.quit
