erezlevi Posted January 17, 2008 Share Posted January 17, 2008 hi, how can I search for a string after doing fileread, and returning the line number where it have been found? Link to comment Share on other sites More sharing options...
Nahuel Posted January 17, 2008 Share Posted January 17, 2008 here's a start: #include <File.au3> $FilePath= $StringToSearch= $CaseSense=0 $Lines=_FileCountLines($FilePath) $hFile=FileOpen($FilePath,0) For $i=0 To Number($Lines) $Test=FileReadLine($hFile,$i) If StringInStr($Test,$StringToSearch,$CaseSense) Then MsgBox(0,"","The string " & $StringToSearch & " was found on line " & $i) ExitLoop EndIf Next FileClose($hFile) Link to comment Share on other sites More sharing options...
weaponx Posted January 17, 2008 Share Posted January 17, 2008 Another option: #include <File.au3> #include <Array.au3> Dim $myArray $StringToSearch = "TEST" _FileReadToArray ( $sFilePath, $myArray) $result = _ArraySearch ($myArray, $StringToSearch, 1) If $result > -1 Then MsgBox(0,"","The string " & $StringToSearch & " was found on line " & $result) EndIf Link to comment Share on other sites More sharing options...
Nahuel Posted January 17, 2008 Share Posted January 17, 2008 Good one. Perhaps a faster one for bigger files. Link to comment Share on other sites More sharing options...
erezlevi Posted January 17, 2008 Author Share Posted January 17, 2008 Good one. Perhaps a faster one for bigger files. hi, it isn't working for me? can you see why? #include <File.au3> #include <Array.au3> Dim $myArray $StringToSearch = "ABKNAME001" $sFilePath="c:\2905_96xxdata.txt" $hFile=FileOpen($sFilePath,0) _FileReadToArray ( $hFile, $myArray) $result = _ArraySearch ($myArray, $StringToSearch, 1) MsgBox (0,"",$result) If $result > -1 Then MsgBox(0,"","The string " & $StringToSearch & " was found on line " & $result) EndIf $r=_ArrayDisplay ($myArray) Link to comment Share on other sites More sharing options...
picaxe Posted January 17, 2008 Share Posted January 17, 2008 (edited) _FileReadToArray ( $hFile, $myArray)Try this, I think you can't use a file handle there _#include <File.au3> #include <Array.au3> Dim $myArray $StringToSearch = "ABKNAME001" $sFilePath="c:\2905_96xxdata.txt" _FileReadToArray ( $sFilePath, $myArray) $result = _ArraySearch ($myArray, $StringToSearch, 1) MsgBox (0,"",$result) If $result > -1 Then MsgBox(0,"","The string " & $StringToSearch & " was found on line " & $result) EndIf Edited January 17, 2008 by picaxe Link to comment Share on other sites More sharing options...
GEOSoft Posted January 17, 2008 Share Posted January 17, 2008 hi, it isn't working for me? can you see why? #include <File.au3> #include <Array.au3> Dim $myArray $StringToSearch = "ABKNAME001" $sFilePath="c:\2905_96xxdata.txt" $hFile=FileOpen($sFilePath,0) _FileReadToArray ( $hFile, $myArray) $result = _ArraySearch ($myArray, $StringToSearch, 1) MsgBox (0,"",$result) If $result > -1 Then MsgBox(0,"","The string " & $StringToSearch & " was found on line " & $result) EndIf $r=_ArrayDisplay ($myArray)Possibly because you are searching for a partial string. The _ArraySearch() call as written will not allow for that. If that's the case then try $result = _ArraySearch ($myArray, $StringToSearch, 1, 0, 0, True) Also see _ArraySearch() in the help file for the meanings of the other parameters. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
erezlevi Posted January 17, 2008 Author Share Posted January 17, 2008 Possibly because you are searching for a partial string. The _ArraySearch() call as written will not allow for that. If that's the case then try $result = _ArraySearch ($myArray, $StringToSearch, 1, 0, 0, True) Also see _ArraySearch() in the help file for the meanings of the other parameters. thanks, it solved it! Link to comment Share on other sites More sharing options...
erezlevi Posted January 17, 2008 Author Share Posted January 17, 2008 Try this, I think you can't use a file handle there _#include <File.au3> #include <Array.au3> Dim $myArray $StringToSearch = "ABKNAME001" $sFilePath="c:\2905_96xxdata.txt" _FileReadToArray ( $sFilePath, $myArray) $result = _ArraySearch ($myArray, $StringToSearch, 1) MsgBox (0,"",$result) If $result > -1 Then MsgBox(0,"","The string " & $StringToSearch & " was found on line " & $result) EndIf you are right, I does not work with filehandel. Link to comment Share on other sites More sharing options...
rudi Posted January 17, 2008 Share Posted January 17, 2008 Hi, #include <File.au3> $FilePath= $StringToSearch= $CaseSense=0 $Lines=_FileCountLines($FilePath) $hFile=FileOpen($FilePath,0) For $i=0 To Number($Lines) <snip> Why do you suggest to use "Number($Lines)" in stead of just "$Lines"? If I got the docs correctly, =_FileCountLines() always returns a numeric value anyways, what do I miss? Regards, Rudi. Earth is flat, pigs can fly, and Nuclear Power is SAFE! Link to comment Share on other sites More sharing options...
weaponx Posted January 17, 2008 Share Posted January 17, 2008 Hi,Why do you suggest to use "Number($Lines)" in stead of just "$Lines"? If I got the docs correctly, =_FileCountLines() always returns a numeric value anyways, what do I miss?Regards, Rudi.I'm not sure why that is there. You don't need it. Maybe Nahuel has been using ActionScript 3.0 too much. 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