PcExpert Posted June 10, 2009 Posted June 10, 2009 (edited) Hi all, I'm currently busy with making a client for a Online RPG. I was thinking for the following: is it possible to search the whole textdocument (created by _IEBodyReadText()) for a specific word and then return the line number? I have found this in the helpfile: $file = FileOpen("test.txt", 0) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Read in lines of text until the EOF is reached While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop MsgBox(0, "Line read:", $line) Wend FileClose($file) This reads the whole file and returns the lines if theres anything on it. I thought about using the StingInStr() function to search the line for a specific string, which is the right way in my opinion. But how to get the linenumber where the string is on to preform a filereadline() after the word has been found? thanks! Edited June 10, 2009 by PcExpert
Valuater Posted June 10, 2009 Posted June 10, 2009 Maybe... expandcollapse popup; demonstration to find chracters that change between to standard points ; or just find a string #include <IE.au3> #include <String.au3> #Region --- IE-Builder generated code Start --- $oIE = _IECreate() ;------------- User input -------------- _IENavigate($oIE, "http://www.autoitscript.com/") ; web address $Find = "Welcome to the " ; my info shows after this line... or just find this line $Before = "- the home " ; my info shows before this line... or set as "" ; ------------ End User input ------------- Sleep(1000) $body = _IEBodyReadHTML($oIE) $sloc = @TempDir & "\stest.txt" FileDelete($sloc) FileWrite($sloc, $body) $sfile = FileOpen($sloc, 0) $num = 0 While 2 $num = $num + 1 $sline = FileReadLine($sfile, $num) If @error Then MsgBox(262208, "Fail", "The string was NOT found ") FileClose($sfile) Exit EndIf If StringInStr($sline, $Find) Then MsgBox(64, "Success", "The string " & $Find & " was found " & @CRLF & " on line # " & $num, 5) If $Before = "" Then ExitLoop $Found = _StringBetween($sline, $Find, $Before) MsgBox(64, "Found", "The string is *" & $Found[0] & "* ", 5) ExitLoop EndIf WEnd #EndRegion --- IE-Builder generated code End --- 8)
Moderators Melba23 Posted June 10, 2009 Moderators Posted June 10, 2009 PcExpert,Have a look at this. It returns a value for the first line of the file in which the required text was found:$text = "What you are looking for" $file = FileOpen("test.txt", 0) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf $nLine = 0 ; Read in lines of text until the EOF is reached While 1 $nLine += 1 $line = FileReadLine($file) If @error = -1 Then $nLine = -1 ExitLoop EndIf If StringInStr($line, $text) Then ExitLoop Wend If $nLine <> -1 Then MsgBox(0, $nLine, FileReadLine($file, $nLine)) FileClose($file)But you already have the text of the line in $line - why do you need to read it again?M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
PcExpert Posted June 10, 2009 Author Posted June 10, 2009 (edited) I need it because the RPG i'm making this client for (NOT a bot!) updates its site often, so the linenumbers where the text is on changes. So instead of using filereadline with a predifined linenumber i'd like to search for the string. An example: If I want to find this: Money: 20,- I currently use filereadline($MYFILENAME, $EXAMPLELINENUMBER) and after that, I use stringsplit() to make it only show the number, so its like this: '20'. But because of the reason mentioned earlier, I wanted to search for the word 'Money: " and then if its found, return the line number so I can use filereadline and stringsplit for further use. This way I dont have to change the predefined linenumbers to read. It saves me alot of work editing the code. Edited June 10, 2009 by PcExpert
Moderators Melba23 Posted June 10, 2009 Moderators Posted June 10, 2009 PcExpert, So does what I posted do the job? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
PsaltyDS Posted June 10, 2009 Posted June 10, 2009 Hi all, I'm currently busy with making a client for a Online RPG. I was thinking for the following: is it possible to search the whole textdocument (created by _IEBodyReadText()) for a specific word and then return the line number? I have found this in the helpfile: $file = FileOpen("test.txt", 0) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Read in lines of text until the EOF is reached While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop MsgBox(0, "Line read:", $line) Wend FileClose($file) This reads the whole file and returns the lines if theres anything on it. I thought about using the StingInStr() function to search the line for a specific string, which is the right way in my opinion. But how to get the linenumber where the string is on to preform a filereadline() after the word has been found? thanks! "Line number" is a fuzzy concept in HTML. Literal @LF and @CR are just ignored white space. Then entire document could be on one line of HTML code, but the "<br>" tags could still present a multi-line document. Still, you could StringSplit() on @CR and then just search the resulting array, if you think the line number matters. Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
PcExpert Posted June 13, 2009 Author Posted June 13, 2009 (edited) Yes, it certainly does work now. Thanks M23 and PSaltyDS for your help. Works like a charm, tested the code for a few days now to see if it really works, but it just does. Thanks again! Edited June 13, 2009 by PcExpert
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