spiky
spiky

Reputation: 13

VBScript to Remove characters from Txt file except particular characters and align the result properly in single line

I am trying to write VBScript to edit a text file, keep only relevent set of numbers in each line and remove all other characters.

Here is the text file

Receive: Return Code: 0x00000000
00000000  08 .               


Receive: Return Code: 0x00000000
00000000  E0 .               


Receive: Return Code: 0x00000000
00000000  07 .               


Receive: Return Code: 0x00000000
00000000  81 .               


Receive: Return Code: 0x00000000
00000000  85 .              


Receive: Return Code: 0x00000000
00000000  32 2               


Receive: Return Code: 0x00000000
00000000  E5 .               


Receive: Return Code: 0x00000000
00000000  1D .               


Receive: Return Code: 0x00000000
00000000  40 @               


Receive: Return Code: 0x00000000
00000000  09 .               


Receive: Return Code: 0x00000000
00000000  05 .               


Receive: Return Code: 0x00000000
00000000  44 D               


Receive: Return Code: 0x00000000
00000000  20                          


Receive: Return Code: 0x00000000
00000000  01 .               


Receive: Return Code: 0x00000000
00000000  00 .               


Receive: Return Code: 0x00000000
00000000  12 .               


Receive: Return Code: 0x00000000
00000000  03 .               


Receive: Return Code: 0x00000000
00000000  00 .               


Receive: Return Code: 0x00000000
00000000  08 .               


Receive: Return Code: 0x00000000
00000000  E0 .           

I need only Numbers starting from E0 to 1D (eight characters from E0) as highlighted in above txt file., and ignore all other until E0 appears again.

Here is the script I wrote

Option explicit
Dim objFSO,objFile,strText,strNewText,strNewText1,strNewText2,strNewText3
Dim strNewText4,strNewText5,Newfile,strNewText6,strNewText7
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("Path\file.txt", ForReading)
strText = objFile.ReadAll
objFile.Close

strNewText = Replace(strText,"00000000"," ")
strNewText1 = Replace(strNewText,"Receive: Return Code: 0x"," ")
strNewText2= Replace(strNewText1,"Receive:"," ")
strNewText3 = replace(strNewText2,"Return Code: 0x"," ")
strNewText4 = replace(strNewText3,","," ")
strNewText5 = replace(strNewText4,"."," ")

Dim illegalChar , Result
Set illegalChar= new Regexp 
illegalChar.ignoreCase = True
illegalChar.Global = True
illegalChar.pattern = "a-zA-Z0_9"
Result = IllegalChar.Replace(strNewText5," ")

strNewText6 = Result 
Set objFile = objFSO.OpenTextFile("Path\result.txt", ForWriting)
objFile.WriteLine strNewText6
objFile.Close

After running above script result is like

08                                                                 



E0                                                                 



07                                                                 



81                                                                 



85                                                                 



32 2               



E5                                                                 



3A :               



42 B               



09                                                                 



05                                                                 



2D -               



05                                                                 



01                                                                 



00                                                                 



12                                                                 



03                                                                 



00                                                                 



08                                                                 



E0

This is my desired output:

E007818532E53A42
E007818532E52C43
...

We need to remove all the characters after the first two digits in each line. Later we can arrange the number starting from E0 for the next 7 double digits in each line and remove all other character until E0 is found again.

Upvotes: 0

Views: 1429

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200373

Something like this should do what you want:

Set fso = CreateObject("Scripting.FileSystemObject")

Set re = New RegExp
re.Pattern = "^0{8}\s+([A-F0-9]{2})"

Set inFile  = fso.OpenTextFile("C:\path\to\file.txt")
Set outFile = fso.OpenTextFile("C:\path\to\result.txt", 2)

Do Until inFile.AtEndOfStream
  line = inFile.ReadLine
  For Each m In re.Execute(line)
    val = m.SubMatches(0)
    If val = "E0" Then
      If Not IsEmpty(str) Then outFile.WriteLine Left(str, 16)
      str = ""
    End If
    If Not IsEmpty(str) Then str = str & val
  Next
Loop
If Not IsEmpty(str) Then outFile.WriteLine Left(str, 16)

inFile.Close
outFile.Close

The regular expression extracts 2-digit hex numbers from each line that begins with 8 zeroes, concatenates them until a value E0 appears, then writes the concatenated string to the output file.

Upvotes: 1

Gurmanjot Singh
Gurmanjot Singh

Reputation: 10360

Just need to apply the logic to fetch all the data contained between E0 and ID until another E0 is found and so on.

Option Explicit
Dim strPath, objFso, objTextFile, objReg, objMatches, match, i, arrTemp, str, temp, strResult, intTempPos, arrTempPattern
strPath = "Path\file.txt"
Set objFso = CreateObject("scripting.Filesystemobject")
Set objTextFile = objFso.OpenTextFile(strPath,1,True)
Set objReg = New RegExp
objReg.Global=True
objReg.IgnoreCase=true

arrTempPattern = array("R.*","[0]{8}\s{2}","\.")
While Not objTextFile.AtEndOfStream
    temp = objTextFile.ReadLine
    For i=0 To UBound(arrTempPattern)
        objReg.Pattern = arrTempPattern(i)
        If objReg.Test(temp) Then
            Set objMatches=objReg.Execute(temp)
            For Each match In objMatches
                temp = Replace(temp,match.Value,"")
            Next
        End If
    Next    
    str = str&temp&vbCrLf
Wend
arrTemp = Split(str,"E0")
If UBound(arrTemp)=0 Then
    str = ""
Else

    For i=1 To UBound(arrTemp)
        strResult = strResult & vbCrLf & "E0"
        intTempPos = InStr(1,arrTemp(i),"1D",1)
        If intTempPos>1 Then
            strResult = strResult & Mid(arrTemp(i),1,intTempPos+1)
        Else
            strResult = strResult & arrTemp(i)
        End If

    Next
End If
Set objTextFile = objFso.OpenTextFile(strPath,2)
objTextFile.Write strResult
objTextFile.Close
Set objTextFile = Nothing
Set objFso = Nothing

Upvotes: 0

Related Questions