Sign in to follow this  
Followers 0

Find a certain string inside a file

14 posts in this topic

Posted (edited)

EDIT (02-09-'10):

I remade the whole function.

See function header for more information. ^_^

#include <Array.au3>

$sFile = @WindowsDir & "\win.ini"
$sString = "a"

$Array = _FileFindString($sFile, $sString)
_ArrayDisplay($Array)

; #FUNCTION# ;===============================================================================
; Name.............: _FileFindString
; Description......: Finds a certain string inside a file.
; Syntax...........: _FileFindString($sOpenFile, $sFindString, $iCase = 0, $iMaxArraySize = 999)
; Parameters.......:    $sOpenFile - The file path to search in
;                       $sFindString - The string to search
;                       $iCase - Flag to indicate if the operations should be case sensitive
;                       $iMaxArraySize - Prepares the returning array size
; Return value.....: Succes - a Array (see remarks)
;                    Failure - Returns wrong parameter and Sets @error:
;                   | 1 - Invalid $sOpenFile
;                   | 2 - Invalid $sFindString
;                   | 3 - Invalid $iCase
;                   | 4 - Invalid $iMaxArraySize
;                   | 5 - FileOpen failed, returns 0
; Author..........: Almar "AlmarM" Mulder (almar dot mulder at live dot com)
; Modified........: -
; Remarks.........: The array returned is two-dimensional and is made up as follows:
;                   $array[0][0] - Array size
;                   $array[0][1] - Number of times the string was found
;                   $array[n][0] - File line number
;                   $array[n][1] - The found string
; Related.........: -
; Link............: -
; Example.........: Yes
; Note............: $iMaxArraySize is used to prepare the returning array size.
;                   If you are almost certain that you'll find more as the default
;                   array size, you could set it to a higher number.
; ===========================================================================================
Func _FileFindString($sOpenFile, $sFindString, $iCase = 0, $iMaxArraySize = 25000)
    If Not IsString($sOpenFile) Then Return SetError(1, 0, $sOpenFile)
    If Not IsString($sFindString) Then Return SetError(2, 0, $sFindString)
    If Not IsInt($iCase) Or $iCase > 1 Then Return SetError(3, 0, $iCase)
    If Not IsInt($iMaxArraySize) Then Return SetError(4, 0, $iMaxArraySize)

    Local $aReturn[$iMaxArraySize][2]
    Local $iTimesFound = 0
    Local $iIndex = 1

    $hOpen = FileOpen($sOpenFile, 0)
    If $hOpen = -1 Then Return SetError(5, 0, 0)

    $sRead = FileRead($hOpen)
    $aMax = StringSplit($sRead, Chr(10))

    For $i = 1 To $aMax[0]
        $sLine = FileReadLine($hOpen, $i)

        If (StringInStr($sLine, $sFindString, $iCase)) Then
            $aReturn[$iIndex][0] = $i
            $aReturn[$iIndex][1] = $sLine

            $aCount = StringSplit($sLine, "")
            For $x = 1 To $aCount[0]
                If ($iCase == 1) Then
                    If ($aCount[$x] == $sFindString) Then $iTimesFound += 1
                ElseIf ($iCase == 0) Then
                    If ($aCount[$x] = $sFindString) Then $iTimesFound += 1
                EndIf
            Next

            $iIndex += 1
        EndIf
    Next
    FileClose($hOpen)

    ReDim $aReturn[$iIndex][2]
    $aReturn[0][0] = UBound($aReturn)
    $aReturn[0][1] = $iTimesFound

    Return $aReturn
EndFunc
Edited by AlmarM

Share this post


Link to post
Share on other sites



Posted

If you are going to use FileOpen() you should close the file.

You are returning the string being searched, in the case above that would be all "a"'s.

Maybe it should return more helpful info like the line number(s) and how many times it was found?

8)

Share this post


Link to post
Share on other sites

Posted (edited)

If you are going to use FileOpen() you should close the file.

You are returning the string being searched, in the case above that would be all "a"'s.

Maybe it should return more helpful info like the line number(s) and how many times it was found?

8)

Yes yes! Great ideas! ^_^

AlmarM

EDIT: Updated! View first post!

Edited by AlmarM

Share this post


Link to post
Share on other sites

Posted

probably we could include common text based file types like ini, txt, log (gotta a be a bit careful , though) , csv, etc

of course, csv would require a bit of more work as csv seperator should be ignored as well..

Share this post


Link to post
Share on other sites

Posted

Remade the whole function.

See first post for more information! ;)

Share this post


Link to post
Share on other sites

Posted

EDIT (02-10-'10):

_ArrayDisplay(_FileFindString($sFile, $sString))

; Description......: Finds a certain string inside a FileChangeDir

I have tried it and it works fine !

3 litle remarks

_The actual month is september.

_ Correct "_ArrayDisplay(_FileFindString($sFile, $sString))", it doesn't work.

_Correct " Finds a certain string inside a FileChangeDir" Posted Image

Share this post


Link to post
Share on other sites

Posted (edited)

I have tried it and it works fine !

3 litle remarks

_The actual month is september.

_ Correct "_ArrayDisplay(_FileFindString($sFile, $sString))", it doesn't work.

_Correct " Finds a certain string inside a FileChangeDir" Posted Image

Oh god, scite made my "file" a "FileChangeDir" ;).

I did september right? Yup, saw it. It's 09 now :)

And the example works for me xD.

Edited by AlmarM

Share this post


Link to post
Share on other sites

Posted

Oh god, scite made my "file" a "FileChangeDir" ;).

I did september right? Yup, saw it. It's 09 now :)

And the example works for me xD.

Not works for me

>Running AU3Check (1.54.19.0)  from:C:\Program Files\AutoIt3
C:\Documents and Settings\Administrateur\Local Settings\Temp\Test_003.au3(6,48) : ERROR: _ArrayDisplay() called with expression on Const ByRef-param(s).
_ArrayDisplay(_FileFindString($sFile, $sString))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Program Files\AutoIt3\Include\Array.au3(322,163) : REF: definition of _ArrayDisplay().
Func _ArrayDisplay(Const ByRef $avArray, $sTitle = "Array: ListView Display", $iItemLimit = -1, $iTranspose = 0, $sSeparator = "", $sReplace = "|", $sHeader = "")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Documents and Settings\Administrateur\Local Settings\Temp\Test_003.au3 - 1 error(s), 0 warning(s)
!>17:37:03 AU3Check ended.rc:2
+>17:37:05 AutoIt3Wrapper Finished
>Exit code: 0    Time: 2.025

This works Posted Image

$_Array =_FileFindString($sFile, $sString)
_ArrayDisplay($_Array)

Share this post


Link to post
Share on other sites

Posted (edited)

Oh, weird. I tried it at work (WinXP x86). I'll try on my own pc (WinVISTA x64) now.

EDIT: Yup, same error.

Edited by AlmarM

Share this post


Link to post
Share on other sites

Posted

I'm on XP sp3 too Posted Image

Share this post


Link to post
Share on other sites

Posted

Updated the first post.

Also, make a little update, $array[0][0] now is array size, $array[0][1] is times the string was found.

Share this post


Link to post
Share on other sites

Posted

After adding Dim $iTimesFoundUBound because error

It give me another error ! Posted Image

+>12:41:36 AU3Check ended.rc:0
>Running:(3.3.6.1):C:\Program Files\AutoIt3\autoit3.exe "C:\Documents and Settings\Administrateur\Local Settings\Temp\Test_033.au3"    
C:\Documents and Settings\Administrateur\Local Settings\Temp\Test_033.au3 (77) : ==> Error in expression.:
$aReturn[0][1] = $iTimesFoundUBound($aReturn)
$aReturn[0][1] = ^ ERROR
->12:41:36 AutoIT3.exe ended.rc:1
+>12:41:38 AutoIt3Wrapper Finished
>Exit code: 1    Time: 1.521

Share this post


Link to post
Share on other sites

Posted

After adding Dim $iTimesFoundUBound because error

It give me another error ! Posted Image

+>12:41:36 AU3Check ended.rc:0
>Running:(3.3.6.1):C:\Program Files\AutoIt3\autoit3.exe "C:\Documents and Settings\Administrateur\Local Settings\Temp\Test_033.au3"    
C:\Documents and Settings\Administrateur\Local Settings\Temp\Test_033.au3 (77) : ==> Error in expression.:
$aReturn[0][1] = $iTimesFoundUBound($aReturn)
$aReturn[0][1] = ^ ERROR
->12:41:36 AutoIT3.exe ended.rc:1
+>12:41:38 AutoIt3Wrapper Finished
>Exit code: 1    Time: 1.521

Whoopsie, edited first post. ;)

Share this post


Link to post
Share on other sites

Posted (edited)

Thanks , works fine now ! Posted Image

Edited by wakillon

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.