Sign in to follow this  
Followers 0
Agent Orange

Parse a Text File

5 posts in this topic

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.

Share this post


Link to post
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

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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  
Followers 0