Jump to content
Sign in to follow this  
Virgil

Help grabbing from text file

Recommended Posts

Virgil

Hi, guys

my text contains this:

0x3,"Classes\AppID\{F808DF63-6049-11D1-BA20-006097D2898E}","AccessPermission",\

01,00,04,80,64,00,00,00,70,00,00,00,00,00,00,00,14,00,\

00,00,02,00,50,00,03,00,00,00,00,00,18,00,01,00,00,00,01,02,00,00,00,00,00,05,\

20,00,00,00,20,02,00,00,00,00,14,00,01,00,00,00,01,01,00,00,00,00,00,05,12,00,\

00,00,00,00,14,00,01,00,00,00,01,01,00,00,00,00,00,05,04,00,00,00,01,01,00,00,\

00,00,00,05,01,01,00,00,00,00,00,05,12,00,00,00,01,01,00,00,00,00,00,05,12,00,\

00,00

I want to save only this portion:

01,00,04,80,64,00,00,00,70,00,00,00,00,00,00,00,14,00,\

00,00,02,00,50,00,03,00,00,00,00,00,18,00,01,00,00,00,01,02,00,00,00,00,00,05,\

20,00,00,00,20,02,00,00,00,00,14,00,01,00,00,00,01,01,00,00,00,00,00,05,12,00,\

00,00,00,00,14,00,01,00,00,00,01,01,00,00,00,00,00,05,04,00,00,00,01,01,00,00,\

00,00,00,05,01,01,00,00,00,00,00,05,12,00,00,00,01,01,00,00,00,00,00,05,12,00,\

00,00

I think the best choice is StringRegExp but I'm lost whith the search patterns. :">

I hope someone point me in the right direction.

Share this post


Link to post
Share on other sites
MadBoy

As you dont precise what you want this is how i understand it.

#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("C:\error.log",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
For $x = 2 to $aRecords[0]
    Msgbox(0,'Record:' & $x, $aRecords[$x])
Next

For $x = 2 to $aRecords[0] ; this line is crucial as it makes your proggy to read from line 2 and ignore first line but that makes some assumptions. That the text you don't want to read is in 1st line of the file.


My little company: Evotec (PL version: Evotec)

Share this post


Link to post
Share on other sites
MadBoy

Or another aproach :)

#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("C:\error.log",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
For $x = 1 to $aRecords[0]
 If StringInStr($aRecords[$x],"\", 1, 3) = 0 Then
  Msgbox(0,'Record:' & $x, $aRecords[$x])
 EndIf
Next

My little company: Evotec (PL version: Evotec)

Share this post


Link to post
Share on other sites
Virgil

Thanks MadBoy.

I'm really confused.

I have a text like this:

0x1,"Classes\AppID\logagent.EXE","AppID","{F808DF63-6049-11D1-BA20-006097D2898E}"

0x3,"Classes\AppID\{F808DF63-6049-11D1-BA20-006097D2898E}","LaunchPermission",\

01,00,04,80,64,00,00,00,70,00,00,00,00,00,00,00,14,00,\

00,00,02,00,50,00,03,00,00,00,00,00,18,00,01,00,00,00,01,02,00,00,00,00,00,05,\

20,00,00,00,20,02,00,00,00,00,14,00,01,00,00,00,01,01,00,00,00,00,00,05,12,00,\

00,00,00,00,14,00,01,00,00,00,01,01,00,00,00,00,00,05,04,00,00,00,01,01,00,00,\

00,00,00,05,01,01,00,00,00,00,00,05,12,00,00,00,01,01,00,00,00,00,00,05,12,00,\

00,00

0x3,"Classes\AppID\{F808DF63-6049-11D1-BA20-006097D2898E}","AccessPermission",\

01,00,04,80,64,00,00,00,70,00,00,00,00,00,00,00,14,00

0x2,"ControlSet001\Services\Eventlog\System\Wudf01000","EventMessageFile","%SystemRoot%\System32\spmsg.dll"

0x3,"Classes\AppID\{F808DF63-6049-11D1-BA20-006097D2898E}","AccessPermission",\

01,00,04,80,64,00,00,00,70,00,00,00,00,00,00,00,14,00

My scope is to sort and delete duplicate entries from text files.

sorting whit _ArraySort the output is like:

00,00

00,00,00,00,14,00,01,00,00,00,01,01,00,00,00,00,00,05,04,00,00,00,01,01,00,00,\

00,00,00,05,01,01,00,00,00,00,00,05,12,00,00,00,01,01,00,00,00,00,00,05,12,00,\

....................

0x1,"Classes\AppID\logagent.EXE","AppID","{F808DF63-6049-11D1-BA20-006097D2898E}"

0x2,"ControlSet001\Services\Eventlog\System\Wudf01000","EventMessageFile","%SystemRoot%\System32\spmsg.dll"

0x3,"Classes\AppID\{F808DF63-6049-11D1-BA20-006097D2898E}","AccessPermission",\

instead desisered output is based to key name

0x1,"Classes\AppID\logagent.EXE","AppID","{F808DF63-6049-11D1-BA20-006097D2898E}"

0x3,"Classes\AppID\{F808DF63-6049-11D1-BA20-006097D2898E}","AccessPermission",\

01,00,04,80,64,00,00,00,70,00,00,00,00,00,00,00,14,00

0x3,"Classes\AppID\{F808DF63-6049-11D1-BA20-006097D2898E}","LaunchPermission",\

01,00,04,80,64,00,00,00,70,00,00,00,00,00,00,00,14,00,\

00,00,02,00,50,00,03,00,00,00,00,00,18,00,01,00,00,00,01,02,00,00,00,00,00,05,\

20,00,00,00,20,02,00,00,00,00,14,00,01,00,00,00,01,01,00,00,00,00,00,05,12,00,\

00,00,00,00,14,00,01,00,00,00,01,01,00,00,00,00,00,05,04,00,00,00,01,01,00,00,\

00,00,00,05,01,01,00,00,00,00,00,05,12,00,00,00,01,01,00,00,00,00,00,05,12,00,\

00,00

0x2,"ControlSet001\Services\Eventlog\System\Wudf01000","EventMessageFile","%SystemRoot%\System32\spmsg.dll"

the only solution that i found is try (without luck) to rewritten the entire text file deleting the ox*, at the beginning of the line and transforming

the 0x3 key like this

0x3,"Classes\AppID\{F808DF63-6049-11D1-BA20-006097D2898E}","AccessPermission",\

01,00,04,80,64,00,00,00,70,00,00,00,00,00,00,00,14,00

to

0x3,"Classes\AppID\{F808DF63-6049-11D1-BA20-006097D2898E}","AccessPermission",\ 01,00,04,80,64,00,00,00,70,00,00,00,00,00,00,00,14,00 .....

after that proceed to delete duplicate entries and sorting.

This is my crap script:

#include <String.au3>
#include <Array.au3>
#include <File.au3>
#NoTrayIcon

Dim $a_cfg = @ScriptDir & '\Log.txt'
Dim $sArray
Global $a_IP[1]

 _FileReadToArray($a_cfg, $sArray)

    For $x = 1 to $sArray[0]
    If @error = -1 Then ExitLoop
    $type = StringLeft($sArray[$x], 3)

    If $type = '0x0' Then
    $sArray[$x] = StringTrimLeft($sArray[$x], 4) & ' #0'
    ElseIf $type = '0x1' Then
    $sArray[$x] = StringTrimLeft($sArray[$x], 4) & ' #1'
    ElseIf $type = '0x2' Then
    $sArray[$x] = StringTrimLeft($sArray[$x], 4) & ' #2'
    ElseIf $type = '0x3' And StringRight($sArray[$x], 2) = ',\' Then
    $key0x3 = $sArray[$x]
    $sArray[$x] = $sArray[$x+1]
       while StringLeft($sArray[$x], 2) = '  '
          $x = $x + 1
          _ArrayAdd($a_IP, $sArray[$x])
          $sArrayString = _ArrayToString( $a_IP,@TAB, 1)
       wend
    $sArray[$x] = StringTrimLeft($key0x3, 4) & $sArrayString & ' #3'
    Dim $a_IP[1] = [0]
    MsgBox(16,"Modded", $sArray[$x])
    ElseIf $type = '0x4' Then
    $sArray[$x] = StringTrimLeft($sArray[$x], 4) & ' #4'
    ElseIf $type = '0x7' Then
    $sArray[$x] = StringTrimLeft($sArray[$x], 4) & ' #7'
    EndIf
    if not $sArray[$x] = "" then
    FileWriteLine(@ScriptDir & '\' & "~tempinf.inf", $sArray[$x])
    endif
    next

   _FileReadToArray("~tempinf.inf", $sArray)
   $Finalarray = _ArrayDeleteClones($sArray)


  Func _ArrayDeleteClones($sArray)
    If Not IsArray($sArray) Then Return SetError(1)
    If UBound($sArray, 0) > 1 Then Return SetError(2)
    Local $NewArr[1], $IsFound=0, $NewArrCnt=0, $Extended=0
    For $i = 1 To UBound($sArray) - 1
        $IsFound = 0
        For $j = 1 To UBound($NewArr)-1
    If $sArray[$i] = $NewArr[$j] Then
              $IsFound = 1
                $Extended += 1
                ExitLoop
            EndIf
       Next
        If Not $IsFound Then
            $NewArrCnt += 1
            ReDim $NewArr[$NewArrCnt+1]
            $NewArr[$NewArrCnt] = $sArray[$i]
        EndIf
    Next
    $NewArr[0] = UBound($NewArr) - 1
    Return SetError(0, $Extended, $NewArr)
  EndFunc


  _ArraySort($Finalarray,0,1)    


 For $x = 1 To UBound($Finalarray)-1
  If StringRight($Finalarray[$x], 2) = '#0' Then
  $Finalarray[$x] = '0x0,'& StringTrimRight($Finalarray[$x], 2)
  endif
  If StringRight($Finalarray[$x], 2) = '#1' Then
  $Finalarray[$x] = '0x1,'& StringTrimRight($Finalarray[$x], 2)
  endif
  If StringRight($Finalarray[$x], 2) = '#2' Then
  $Finalarray[$x] = '0x2,'& StringTrimRight($Finalarray[$x], 2)
  endif
  If StringRight($Finalarray[$x], 2) = '#3' Then
  $Finalarray[$x] = '0x3,'& StringTrimRight($Finalarray[$x], 2)
  endif
  If StringRight($Finalarray[$x], 2) = '#4' Then
  $Finalarray[$x] = '0x4,'& StringTrimRight($Finalarray[$x], 2)
  endif
  If StringRight($Finalarray[$x], 2) = '#7' Then
  $Finalarray[$x] = '0x7,'& StringTrimRight($Finalarray[$x], 2)
  endif
  If not $Finalarray[$x] = "" then
  FileWriteLine(@ScriptDir & '\Result.txt', $Finalarray[$x])
  endif
Next

Sorry for my poor english and for long post.

If someone give me any guidance is really appreciate.

My regards.

Share this post


Link to post
Share on other sites
Virgil

Hi,

After reading and reading the forum :"> I found what I need ...

I'm stuck only transforming the 0x3 relates key like this:

0x3,"Classes\AIFFFile","EditFlags",\
  00,00,01,00

0x3,"Classes\AppID\{F808DF63-6049-11D1-BA20-006097D2898E}","AccessPermission",\
  01,00,04,80,64,00,00,00,70,00,00,00,00,00,00,00,14,00,\
  00,00,02,00,50,00,03,00,00,00,00,00,18,00,01,00,00,00,01,02,00,00,00,00,00,05,\
  20,00,00,00

into One single line like this:

0x3,"Classes\AIFFFile","EditFlags",\ 00,00,01,00

0x3,"Classes\AppID\{F808DF63-6049-11D1-BA20-006097D2898E}","AccessPermission",\ 01,00,04,80,64,00,00,00,70,00,00,00,00,00,00,00,14,00,\ ..........

Please Help A Noob !

Share this post


Link to post
Share on other sites
Davo

Virgil give me the code that you used to transform the duplicate entries into the ones you need. I think there is a delimiter or something that is making your code be split at every "\" and be stored in a seperate array element, then when you are writing it you are ending up with things spanning across more then one line.

This thread is all a bit confusing lol

Dave


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------"I don't need to know everything, I just need to know where to find it when I need it"....EinsteinAnd in our case... That's the AutoIT helpfile ;) Please read before posting!!!

Share this post


Link to post
Share on other sites
Virgil

Hi, Davo

You are right This thread is all a bit confusing ...my apologies.

$s_IPListPath = @ScriptDir & '\text.txt'
_FileReadToArray($s_IPListPath, $a_ListTemp)

For $i = 1 To UBound($a_ListTemp) - 1 
Select
Case StringLeft($a_ListTemp[$i], 3) = '0x3' and StringRight($a_ListTemp[$i+1], 2) <> ',\'        ; only one subkey
$a_ListTemp[$i] = $a_ListTemp[$i] & @LF & $a_ListTemp[$i+1]

Case StringLeft($a_ListTemp[$i], 3) = '0x3' and StringRight($a_ListTemp[$i+1], 2) = ',\'   ; I'm lost here

Share this post


Link to post
Share on other sites
Davo

Ok so basically you are trying to eliminate lines that are identical?

If this is the case I have some code for you however it might be a bit slow...

Dave


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------"I don't need to know everything, I just need to know where to find it when I need it"....EinsteinAnd in our case... That's the AutoIT helpfile ;) Please read before posting!!!

Share this post


Link to post
Share on other sites
Virgil

Ok so basically you are trying to eliminate lines that are identical?

If this is the case I have some code for you however it might be a bit slow...

Dave

Yes and thank for your time.

Share this post


Link to post
Share on other sites
Davo

Ok this will basically do what I think you need it to do without confusion

#include<File.au3>

Dim $filePath = "C:\whatever\file.txt" ;MAKE THIS YOUR FILE PATH TO THE TEXT FILE
Dim $numberOfLinesInText
Dim $lineArray[1]
Dim $i
Dim $j
Dim $testLine
Dim $newText

$numberOfLinesInText = _FileCountLines($filePath)
If $numberOfLinesInText <> 0 Then
    ReDim $lineArray[$numberOfLinesInText]
Else
    ;an error occured lets get the hell out of here before the boss finds out
    Exit
EndIf

;open the file for reading
$fileHandle = FileOpen($filePath,0)

;store all the lines into the array
For $i = 1 To $numberOfLinesInText Step 1
    $lineArray[$i - 1] = FileReadLine($fileHandle,$i)
Next

;close the file
FileClose($fileHandle)

;compare the elements of the array to see if any duplicates exist
For $i = 0 To UBound($lineArray) - 1 Step 1
    $testLine = $lineArray[$i]
    For $j = 0 To UBound($lineArray) - 1 Step 1
        If $lineArray[$j] == $testLine Then
            $lineArray[$j] = ""
        EndIf
    Next
Next

;write the lines back to the text file easaring previous contents
$fileHandle = FileOpen($filePath,2)
For $i = 0 To UBound($lineArray) - 1 Step 1
    If $lineArray[$i] <> "" Then
        $newText &= $lineArray[$i] & @CRLF
    EndIf
Next

FileWrite($fileHandle,$newText)

FileClose($fileHandle)

Exit

Hope this is what your after

NOTE:THIS CODE IS NOT TESTED!!!!

Dave

Edited by Davo

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------"I don't need to know everything, I just need to know where to find it when I need it"....EinsteinAnd in our case... That's the AutoIT helpfile ;) Please read before posting!!!

Share this post


Link to post
Share on other sites
Davo

TESTED AND WORKING CODE

#include<File.au3>

Dim $filePath = "c:\test.txt" ;MAKE THIS YOUR FILE PATH TO THE TEXT FILE
Dim $numberOfLinesInText
Dim $lineArray[1]
Dim $i
Dim $j
Dim $testLine
Dim $newText

$numberOfLinesInText = _FileCountLines($filePath)
If $numberOfLinesInText <> 0 Then
    ReDim $lineArray[$numberOfLinesInText]
Else
    MsgBox(0,"","ERROR")
    ;an error occured lets get the hell out of here before the boss finds out
    Exit
EndIf

;open the file for reading
$fileHandle = FileOpen($filePath,0)

;store all the lines into the array
For $i = 1 To $numberOfLinesInText Step 1
    $lineArray[$i - 1] = FileReadLine($fileHandle,$i)
Next

;close the file
FileClose($fileHandle)

;compare the elements of the array to see if any duplicates exist
For $i = 0 To UBound($lineArray) - 1 Step 1
    $testLine = $lineArray[$i]
    For $j = $i + 1 To UBound($lineArray) - 1 Step 1
        If $lineArray[$j] == $testLine Then
            $lineArray[$j] = ""
        EndIf
    Next
Next

;write the lines back to the text file easaring previous contents
$fileHandle = FileOpen($filePath,2)
For $i = 0 To UBound($lineArray) - 1 Step 1
    If $lineArray[$i] <> "" Then
        $newText &= $lineArray[$i] & @CRLF
    EndIf
Next

FileWrite($fileHandle,$newText)

FileClose($fileHandle)

Exit

Dave


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------"I don't need to know everything, I just need to know where to find it when I need it"....EinsteinAnd in our case... That's the AutoIT helpfile ;) Please read before posting!!!

Share this post


Link to post
Share on other sites
Virgil

No.

Put this in the file.txt

0x3,"Classes\AIFFFile","EditFlags",\

00,00,01,00

0x3,"Classes\AIFFFile","EditFlags",\

00,00,01,00

Share this post


Link to post
Share on other sites
Davo

Try the new code I just posted.

Sorry the first one didn't work.

Dave :)


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------"I don't need to know everything, I just need to know where to find it when I need it"....EinsteinAnd in our case... That's the AutoIT helpfile ;) Please read before posting!!!

Share this post


Link to post
Share on other sites
Virgil

Now try this:

0x3,"Classes\AIFFFile","EditFlags",\
  00,00,01,00
0x1,"Classes\adbanner.adbanner","","adbanner Class"
0x3,"Classes\AIFFFile","EditFlags",\
  00,00,01,00

the output is:

0x3,"Classes\AIFFFile","EditFlags",\
0x1,"Classes\adbanner.adbanner","","adbanner Class"
  00,00,01,00

each

0x3,"Classes\AIFFFile","EditFlags",\         must be considered like 1 element
  00,00,01,00

this is the reason for which I try to convert into

0x3,"Classes\AIFFFile","EditFlags",\ 00,00,01,00

Share this post


Link to post
Share on other sites
Davo

ohhh ok now its making a bit more sense :)

Ok give me a minute I will see if i can change my code accordingly

Dave


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------"I don't need to know everything, I just need to know where to find it when I need it"....EinsteinAnd in our case... That's the AutoIT helpfile ;) Please read before posting!!!

Share this post


Link to post
Share on other sites
Davo

Virgil one question before I post the next modification to the code.

If the line starts with 0x2 and then another starts with 0x3 are the different lines? or are they equal?

Dave


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------"I don't need to know everything, I just need to know where to find it when I need it"....EinsteinAnd in our case... That's the AutoIT helpfile ;) Please read before posting!!!

Share this post


Link to post
Share on other sites
Davo

This should finally work :)

#include<File.au3>

Dim $filePath = "C:\Documents and Settings\Dave\My Documents\Automations\Forum Help\test.txt" ;MAKE THIS YOUR FILE PATH TO THE TEXT FILE
Dim $numberOfLinesInText
Dim $lineArray[1]
Dim $i
Dim $j
Dim $testLine
Dim $newText
Dim $tempText

$numberOfLinesInText = _FileCountLines($filePath)
If $numberOfLinesInText <> 0 Then
    ReDim $lineArray[$numberOfLinesInText]
Else
    MsgBox(0,"","ERROR")
    ;an error occured lets get the hell out of here before the boss finds out
    Exit
EndIf   

;open the file for reading
$fileHandle = FileOpen($filePath,0)

;rewrite the text file with the lines ending in '\ being concantinated with the line under them
$i = 1
$j = 0
$newText = ""
Do
    $tempText = FileReadLine($filePath,$i)
    If StringRight($tempText,2) == ',\' Then
        $newText &= $tempText
    Else
        $newText &= $tempText
        $lineArray[$j] = $newText
        $newText = ""
        $j += 1
    EndIf
    $i += 1
Until $i > $numberOfLinesInText

FileClose($fileHandle)

;open the file to write the new concantinated text
$fileHandle = FileOpen($filePath,2)

ReDim $lineArray[$j]

For $j = 0 To UBound($lineArray) - 1 Step 1
    $newText &= $lineArray[$j] & @CRLF
Next

;write the text to the file and cross your fingers
FileWrite($fileHandle,$newText)

;close the file
FileClose($fileHandle)

;count the lines in the new text file
$numberOfLinesInText = _FileCountLines($filePath)

If $numberOfLinesInText <> 0 Then
    Dim $lineArray[$numberOfLinesInText]
Else
    MsgBox(0,"","ERROR")
    ;an error occured lets get the hell out of here before the boss finds out
    Exit
EndIf

;open the file for reading
$fileHandle = FileOpen($filePath,0)

;store all the lines into the array
For $i = 1 To $numberOfLinesInText Step 1
    $lineArray[$i - 1] = FileReadLine($fileHandle,$i)
Next

;close the file
FileClose($fileHandle)

;compare the elements of the array to see if any duplicates exist
For $i = 0 To UBound($lineArray) - 1 Step 1
    $testLine = $lineArray[$i]
    For $j = $i + 1 To UBound($lineArray) - 1 Step 1
        If $lineArray[$j] == $testLine Then
            $lineArray[$j] = ""
        EndIf
    Next
Next

$newText = ""

;write the lines back to the text file easaring previous contents
$fileHandle = FileOpen($filePath,2)
For $i = 0 To UBound($lineArray) - 1 Step 1
    If $lineArray[$i] <> "" Then
        $newText &= $lineArray[$i] & @CRLF
    EndIf
Next

FileWrite($fileHandle,$newText)

FileClose($fileHandle)

Exit

Dave


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------"I don't need to know everything, I just need to know where to find it when I need it"....EinsteinAnd in our case... That's the AutoIT helpfile ;) Please read before posting!!!

Share this post


Link to post
Share on other sites
Virgil

Hi, Davo

All seems OK :)

Thanks for your help, really appreciated.

Edited by Virgil

Share this post


Link to post
Share on other sites
Davo

Your welcome,i'm just glad we sorted it out

Davo :)


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------"I don't need to know everything, I just need to know where to find it when I need it"....EinsteinAnd in our case... That's the AutoIT helpfile ;) Please read before posting!!!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×