Hypertrophy Posted August 1, 2009 Posted August 1, 2009 how can i read a text file and search for a certain string and find the last occurrence of it? for example if my text file contains 1000 lines of data and the word "Table Settings of Game" appears 300 times in the file how can i find the last occurence of that string. Thanks for your help in advance.
Developers Jos Posted August 1, 2009 Developers Posted August 1, 2009 how can i read a text file and search for a certain string and find the last occurrence of it? for example if my text file contains 1000 lines of data and the word "Table Settings of Game" appears 300 times in the file how can i find the last occurence of that string. Thanks for your help in advance.You are long enough here to know you have to show some effort yourself first.So... what have you tried ?Jos 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.
Hypertrophy Posted August 3, 2009 Author Posted August 3, 2009 Jos, $file = "test.txt" ; file i want to read from If StringInStr($file, "Table Settings of Game") Then MsgBox(0,"","Found String, reading downward from this string...") EndIf But of course the above just looks to see if that string is anywhere in the file, I want it to find the last occurence and then read downward from that point.
AdmiralAlkex Posted August 3, 2009 Posted August 3, 2009 Jos, $file = "test.txt" ; file i want to read from If StringInStr($file, "Table Settings of Game") Then MsgBox(0,"","Found String, reading downward from this string...") EndIf But of course the above just looks to see if that string is anywhere in the file, I want it to find the last occurence and then read downward from that point. So why are you not telling StringInStr() to find the last occurance? >_< .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface
Hypertrophy Posted August 3, 2009 Author Posted August 3, 2009 (edited) Is that possible? I know there is a occurence parameter but would I manually have to specify the occurence? Or could I use -1 to find the last occurence? How do I do that? Edited August 3, 2009 by Hypertrophy
Hypertrophy Posted August 4, 2009 Author Posted August 4, 2009 So why are you not telling StringInStr() to find the last occurance? >_<HOW DO I DO THIS
BrettF Posted August 4, 2009 Posted August 4, 2009 You cannot read the helpfile? COME ON! $string = "I want to game. I need to game. Game!" $sSearch = "game" $pos = StringInStr ($string, $sSearch, 2, -1) MsgBox (0, "", "String found at pos: " & $pos) >_> Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version!
Hypertrophy Posted August 4, 2009 Author Posted August 4, 2009 This'll only work for the last occurrence on a single line. My text file may be 1000 lines of data, like in my OP.
water Posted August 4, 2009 Posted August 4, 2009 (edited) Something like (pseudocode): $lines = _FileCountLines() for $i = $lines to 1 step -1 $line = FileReadline(filehandle,$i) if stringinstr($line,"your search string") = 0 then msgbox(0,"","blablabla") exitloop endif next Edited August 4, 2009 by water My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
BrettF Posted August 4, 2009 Posted August 4, 2009 Oh will it? $string = "I want to game. I need to game. Game!" & @CRLF $string &= "I want to game. I need to game. Game!" & @CRLF $string &= "I want to game. I need to game. Game!" $sSearch = "game" $pos = StringInStr ($string, $sSearch, 2, -1) MsgBox (0, "", "String found at pos: " & $pos) Maybe if you understood that @CRLF was a character (OMG NO WAYZ!), you would realize that you're being stupid. But then again, with someone with <200 posts, I would expect them to at least try the fucking examples. Far out. Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version!
trancexx Posted August 4, 2009 Posted August 4, 2009 Oh will it? $string = "I want to game. I need to game. Game!" & @CRLF $string &= "I want to game. I need to game. Game!" & @CRLF $string &= "I want to game. I need to game. Game!" $sSearch = "game" $pos = StringInStr ($string, $sSearch, 2, -1) MsgBox (0, "", "String found at pos: " & $pos) Maybe if you understood that @CRLF was a character (OMG NO WAYZ!), you would realize that you're being stupid. But then again, with someone with <200 posts, I would expect them to at least try the fucking examples. Far out. You are using wrong inequality sign. ♡♡♡ . eMyvnE
Hypertrophy Posted August 4, 2009 Author Posted August 4, 2009 Gosh, I wish I could curse you out too, but unfortunately I can't at the moment because I still need some help. Anyway, I didn't understand that -1 would still work on completely new lines. I am new in this file reading parsing etc territory. So from your code it would return the position of the last occurrence of "game"? Am I correct? How would I take that position and read downward until I reach the end of the file?
water Posted August 4, 2009 Posted August 4, 2009 What BrettF suggests is to read the whole file into one variable and then serach for the last occurrance of the search string. So use FileRead to read the whole file into a variable and then use BrettF's code. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
enaiman Posted August 4, 2009 Posted August 4, 2009 It is obvious the OP has some issues with reading the help (I guess either he's not reading it or he doesn't read it attentively). The living proof of my statement is in this quote from StringInStr occurrence [optional] Which occurrence of the substring to find in the string. Use a negative occurrence to search from the right side. The default value is 1 (finds first occurrence). I have tried to help him several days ago but in the end it proved to be a waste of my time so I've stayed out of his new threads. BrettF - I understand your frustration mate, it's a normal reaction. SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :)
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