Jump to content
Sign in to follow this  
390390390

Finding data in a text file to be used as a variable

Recommended Posts

390390390

I have been reading through AutoIt forums for a whole day now, and I figure I should just ask my question rather than tear my hair out seeing if I can find a needle in a haystack.

I have a text file (C:\relay.txt) and an AutoIt script that controls another, very simple, executable (not AutoIt). The text file contains the following line:

Serial=12345

The first half (the key) will never change, but it is not always at the same place in the text file. So I simply want to search the .txt file for the string "Serial=" and then create a variable with the data that follows it (through the end of the line), eg "12345." I will use this variable in my AutoIt script to input that data into the other executable.

I feel as though the key lies somewhere in FileRead, FileOpen, String, StringRegExp, but I'm not sure. Again, I'm just looking for the simplest way to grab a variable from a text file. Going the .ini route is not possible in this instance.

Thanks for any help you can offer.

-J

Share this post


Link to post
Share on other sites
youknowwho4eva

I would read the line,

(look at file read line)

trim the all except the first 7 characters

(I'll help you with this one stringtrimleft($line,(stringlen($line) - 7))

compare to see if it says Serial=

(If $linetest = "Serial=" then)

Then you do the inverse to that same line to get the serial number

(Stringtrimright($line,7))


Giggity

Share this post


Link to post
Share on other sites
390390390

I would read the line,

(look at file read line)

trim the all except the first 7 characters

(I'll help you with this one stringtrimleft($line,(stringlen($line) - 7))

compare to see if it says Serial=

(If $linetest = "Serial=" then)

Then you do the inverse to that same line to get the serial number

(Stringtrimright($line,7))

My code, which is not working yet, looks like this:

CODE
$CONF = FileOpen( $CONFPATH & "config.txt", 0 )

$SITE = ( FileReadLine ($CONF) )

IF $SERIALTEST = "Serial="

Then (StringTrimLeft ($SITE, 7)

EndIf

I did play around with the FileReadLine, and I got it to work in the context of telling the script which line to read, but I still cannot make the If...then statement work. Any suggestions?

Share this post


Link to post
Share on other sites
MrMitchell

This is kind of sloppy, but works for me:

While 1
    $nextLine = FileReadLine($hFile)
    If @error = -1 Then ExitLoop
    If StringInStr($nextLine, "Serial=") Then
        $aLine = StringSplit($nextLine, "Serial=", 1)
        If UBound($aLine) > 2 Then
            For $i = 2 To (UBound($aLine) - 1)
                $serial &= $aLine[$i]
            Next
        Else
            $serial = $aLine[2]
        EndIf
        ExitLoop
    EndIf
WEnd

MsgBox(0, "Serial=", $serial)

Basically loops through the file until either the end of file is reached or it finds "Serial=" somewhere in one of the lines. If it find the "Serial=" then it will split the line on "Serial=", ignore the first part then return the rest of the line as $serial starting with the first character after the "=".

Share this post


Link to post
Share on other sites
Spiff59

Or...

Dim $serial
While Not $serial 
    $nextLine = FileReadLine($hFile)
    If @error Then ExitLoop
    If StringInStr($nextLine, "Serial=") Then $serial = StringTrimLeft($nextLine, 7)
WEnd
Msgbox(1,"",$serial)

Share this post


Link to post
Share on other sites
hunt

$ReadFile=FileRead("C:\relay.txt")
$serial=StringRegExp($ReadFile,"Serial=(.+)\h*\v*",1)
If not @error Then 
   $serial=$serial[0]
   MsgBox(0,"",$serial)
Else
   MsgBox(16,"Error","Not found serial")
Endif

Edited by hunt

Share this post


Link to post
Share on other sites
MrMitchell

Or...

Dim $serial
While Not $serial 
    $nextLine = FileReadLine($hFile)
    If @error Then ExitLoop
    If StringInStr($nextLine, "Serial=") Then $serial = StringTrimLeft($nextLine, 7)
WEnd
Msgbox(1,"",$serial)
LoL DUH to me about the StringTrimLeft. I think youknowwho4eva did that too and it totally went right by me, but that that's better than StringSplit.

About using StringInStr and reading every line as opposed to reading the entire file into a string and using StringRegExp. Which is better in terms of efficiency? Or does it depend on the file size? I guess StringRegExp is cool as long as the file isn't so large that its total size exceeds the maximum that a string will hold?

Share this post


Link to post
Share on other sites
390390390

Or...

Dim $serial
While Not $serial 
    $nextLine = FileReadLine($hFile)
    If @error Then ExitLoop
    If StringInStr($nextLine, "Serial=") Then $serial = StringTrimLeft($nextLine, 7)
WEnd
Msgbox(1,"",$serial)
Thanks, Spiff. This works perfectly, and since my text file is relatively small (15-20 lines), I think the FileReadLine will work well. Thank you to everyone who contributed. This really helped my understanding of finding variables outside of a script. -J

Share this post


Link to post
Share on other sites
Spiff59

Actually, since the StringTrimLeft assumes that "Serial=" was in the first seven bytes, but the StringInStr will get a hit on "Serial=" anywhere in a line, it probably would have been more appropriate to go this route:

Dim $serial
While Not $serial 
    $nextLine = FileReadLine($hFile)
    If @error Then ExitLoop
    If (StringLeft($nextLine, 7) = "Serial=") Then $serial = StringTrimLeft($nextLine, 7)
WEnd
Msgbox(1,"",$serial)

StringLeft is likely a little faster than StringInStr as well.

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.