Jump to content

searching string inside a file and returning line number


 Share

Recommended Posts

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

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

_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 by picaxe
Link to comment
Share on other sites

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

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! :D
Link to comment
Share on other sites

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

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

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

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...