390390390 Posted April 21, 2009 Share Posted April 21, 2009 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 Link to comment Share on other sites More sharing options...
youknowwho4eva Posted April 21, 2009 Share Posted April 21, 2009 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 Link to comment Share on other sites More sharing options...
390390390 Posted April 21, 2009 Author Share Posted April 21, 2009 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)EndIfI 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? Link to comment Share on other sites More sharing options...
MrMitchell Posted April 22, 2009 Share Posted April 22, 2009 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 "=". Link to comment Share on other sites More sharing options...
MrMitchell Posted April 22, 2009 Share Posted April 22, 2009 Got rid of some unnecessary lines: 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 $serial = $aLine[2] ExitLoop EndIf WEnd Link to comment Share on other sites More sharing options...
Spiff59 Posted April 22, 2009 Share Posted April 22, 2009 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) Link to comment Share on other sites More sharing options...
hunt Posted April 22, 2009 Share Posted April 22, 2009 (edited) $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 April 22, 2009 by hunt Link to comment Share on other sites More sharing options...
MrMitchell Posted April 22, 2009 Share Posted April 22, 2009 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? Link to comment Share on other sites More sharing options...
390390390 Posted April 22, 2009 Author Share Posted April 22, 2009 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 Link to comment Share on other sites More sharing options...
Spiff59 Posted April 22, 2009 Share Posted April 22, 2009 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now