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, 04 September 2010 - 10:56 AM.





