' VBScript source code 'For Extrcting XXXX.XXXX account totals in the file 'Works for both the formats of input '------------------Main Body--------------------------------------------------- Option Explicit 'File Operations Related Dim ForReading, ForWriting Dim FSO, Folder, FileNameCollection Dim fin, fout 'Variables Dim Source, Destination Dim InputFileName , OutputFileName Dim line, newLine Dim UnitName, AccountName Dim UnitNumber, AccountTotal, AccountNumber Dim AcFlag Dim i,j 'Regular Expression Variables Dim regExObjUnit, regExObjAccountTotal, regExAcNum, regExAcTot Dim MatchObjCol, MatchObj 'Pattern Related Dim COMA_SPERATED_REAL Dim COMA_SPERATED_REAL_IN_PARANTHESIS Dim REAL_NUMBER_PATTERN Dim MIN_PRECISION Dim MAX_PRECISION Dim BLANK_SPACES ForReading = 1 ForWriting = 2 InitializePattern 'Open/Create the files needed '---------------------------------- InputFileName = InputBox("Enter the Source File Name(Include Full Path)","Input","C:\Scripts\Source\FAR_merge.txt") OutputFileName= InputBox("Enter the Output File Name(Include Full Path)","Output","C:\Scripts\Source\output2.tsv") Set FSO = CreateObject("Scripting.FileSystemObject") set fin = FSO.OpenTextFile(InputFileName,ForReading,False) set fout = FSO.OpenTextFile(OutputFileName,ForWriting,true) 'Set the UNIT Regular Expression set regExObjUnit = new RegExp regExObjUnit.pattern = "Unit:" regExObjUnit.IgnoreCase = true regExObjUnit.Global = false 'Set the ACCOUNT TOTAL Regular Expression set regExObjAccountTotal = new RegExp regExObjAccountTotal.pattern = "ACCOUNT TOTAL" regExObjAccountTotal.IgnoreCase = true regExObjAccountTotal.Global = false Set regExAcNum = New RegExp Set regExAcTot = New RegExp regExAcTot.Pattern = COMA_SPERATED_REAL_IN_PARANTHESIS & "|" & COMA_SPERATED_REAL 'Initialize UnitName = "" UnitNumber = 0 AccountTotal = 0 AccountName = "" AccountNumber = 0 AcFlag = FALSE fout.writeLine "UnitNumber " & vbTab & "AccountName " & vbTab & "AccountNumber" & vbTab & "AccountTotal" Do While fin.AtEndOfStream <> True line = fin.readline 'Look for Unit in the line If regExObjUnit.Test(line) then 'Unit: exists - Write the Unit Number and Name to the OUTPUT file set MatchObjCol = regExObjUnit.Execute(line) set MatchObj = MatchObjCol(0) i= MatchObj.FirstIndex + MatchObj.Length j= Mid(line,i+2,8) If j<>UnitNumber Then UnitNumber = j regExObjUnit.pattern = "Name:" set MatchObjCol = regExObjUnit.Execute(line) set MatchObj = MatchObjCol(0) i = MatchObj.FirstIndex + MatchObj.Length UnitName = Mid(line,i+1) regExObjUnit.pattern = "Unit:" End If Else 'Unit Doesnt Exit in this Line. 'Check for the Account Name and Number regExAcNum.Pattern = "\d\d\d\d\.\d\d\d\d" If regExAcNum.Test(Line) = TRUE Then Set MatchObjCol = regExAcNum.Execute(Line) AccountNumber = MatchObjCol(0) AccountName = Mid(line,1,MatchObjCol(0).FirstIndex) newLine = UnitNumber & vbTab & AccountName & vbTab & AccountNumber AcFlag = TRUE End If 'Check if there is Account Total If regExObjAccountTotal.Test(line) and AcFlag Then If regExAcTot.Test(Line) Then Set MatchObjCol = regExAcTot.Execute(Line) AccountTotal = MatchObjCol(0) newLine = newLine &vbTab & AccountTotal fout.writeline newLine AccountTotal = 0 AccountName = "" AccountNumber = 0 AcFlag = FALSE End If End If End if Loop MsgBox "ExtracTion Complete",,"Extractor" Sub InitializePattern() Dim i, j Dim p1,p2,p3,p4,p5 Dim Decimal_Part p1 = "\d+" p2 = "\d+,\d+" p3 = "\d+,\d+,\d+" p4 = "\d+,\d+,\d+,\d+" p5 = "\d+,\d+,\d+,\d+,\d+" MIN_PRECISION = 2 MAX_PRECISION = 2 BLANK_SPACES = " {0,}" REAL_NUMBER_PATTERN = "\d+\.\d+" Decimal_Part = "\.\d{" & MIN_PRECISION & "," & MAX_PRECISION & "}" COMA_SPERATED_REAL= p5 & Decimal_Part & "|" & p4 & Decimal_Part & "|" & p3 & Decimal_Part & "|" & p2 & Decimal_Part & "|" & p1 & Decimal_Part COMA_SPERATED_REAL_IN_PARANTHESIS = "\(" & BLANK_SPACES & p5 & Decimal_Part & BLANK_SPACES & "\)|\(" & BLANK_SPACES & _ p4 & Decimal_Part & BLANK_SPACES & "\)|\(" & BLANK_SPACES & _ p3 & Decimal_Part & BLANK_SPACES & "\)|\(" & BLANK_SPACES & _ p2 & Decimal_Part & BLANK_SPACES &"\)|\(" & BLANK_SPACES & _ p1 & Decimal_Part & BLANK_SPACES & "\)" End Sub