Jump to content

Parse a Text File


Recommended Posts

Ok, I have a text file that might have more than one entry that I need to save in a variable(s). The information I need is in the middle of the line(s) and is not always the same text. Here is a sample of the text batch file I need to read.

@ECHO OFF

IF not Exist z:\capture.exe GOTO END

CLS

Echo Printer Setup...

Z:\CAPTURE L=1 Q=.ir-lj4-b04.context.sa /TI=10 /NB /NFF /NT

Z:\CAPTURE L=2 Q=.ir-lj5-b10.context2.sa /TI=10 /NB /NFF /NT

:END

EXIT

Notice that in this file example there are two capture statements. I need to read this file and assign the data between the q= and /TI entry on both lines to variables.

So in this case .ir-lj4-b04.context.sa and .ir-lj5-b10.context2.sa are what I need.

Any help would be greatly appreciated.

Link to comment
Share on other sites

  • Developers

Modified Helpfile example:

$content=Fileread("test.txt")
$nOffset = 1
While 1
    $array = StringRegExp($content, '(?i)Q=(.*?) /TI=', 1, $nOffset)
    
    If @error = 0 Then
        $nOffset = @extended
    Else
        ExitLoop
    EndIf
    for $i = 0 to UBound($array) - 1
        msgbox(0, "RegExp Test with Option 1 - " & $i, $array[$i])
    Next
WEnd

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Not perfect but try this also:

#include <Array.au3>

#include <File.AU3>

$Txt_File="Batch.txt"

Dim $File_Array

_FileReadToArray(@ScriptDir & "\" & $Txt_File,$File_Array)

;_ArrayDisplay($File_Array,"")

For $x=1 to $File_Array[0]

$Q_String=StringInStr($File_Array[$x],"Q=")

$TI_String=StringInStr($File_Array[$x],"/TI")

If $Q_String<>0 and $TI_String<>0 then

$String_Array=StringSplit($File_Array[$x]," ")

;_ArrayDisplay($String_Array,"")

MsgBox(4096,"",$String_Array[3] & @CRLF & $String_Array[4])

EndIf

Next

Link to comment
Share on other sites

Modified Helpfile example:

$content=Fileread("test.txt")
$nOffset = 1
While 1
    $array = StringRegExp($content, '(?i)Q=(.*?) /TI=', 1, $nOffset)
    
    If @error = 0 Then
        $nOffset = @extended
    Else
        ExitLoop
    EndIf
    for $i = 0 to UBound($array) - 1
        msgbox(0, "RegExp Test with Option 1 - " & $i, $array[$i])
    Next
WEnd
JdeB-

This is very close to what I want. However, it can't get it to assign a new array value, $i, for each entry. If I run this, even the AutoIT example, the value of $i is always zero. How can I get it to add one on each time without an error?

Thanks

Link to comment
Share on other sites

  • Developers

JdeB-

This is very close to what I want. However, it can't get it to assign a new array value, $i, for each entry. If I run this, even the AutoIT example, the value of $i is always zero. How can I get it to add one on each time without an error?

Thanks

Just create your own array and add all found instances to it at the point where the MSGBOX is located... shouldn't be too hard :shocked: Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...