Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Find a certain string inside a file


  • Please log in to reply
13 replies to this topic

#1 AlmarM

AlmarM

    Programming my way.

  • Active Members
  • PipPipPipPipPipPip
  • 1,690 posts

Posted 24 April 2009 - 04:00 PM

EDIT (02-09-'10):

I remade the whole function.
See function header for more information. ^_^

AutoIt         
#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.

Minesweeper

A minesweeper game created in autoit, source available.

 

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

 

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.








#2 Valuater

Valuater

    www.PayFreeWireless.com

  • MVPs
  • 11,196 posts

Posted 24 April 2009 - 04:28 PM

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)

Posted Image

Clic The Pic!!!


#3 AlmarM

AlmarM

    Programming my way.

  • Active Members
  • PipPipPipPipPipPip
  • 1,690 posts

Posted 24 April 2009 - 06:02 PM

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, 24 April 2009 - 06:13 PM.

Minesweeper

A minesweeper game created in autoit, source available.

 

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

 

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.


#4 rajeshontheweb

rajeshontheweb

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 382 posts

Posted 25 April 2009 - 07:36 PM

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..

#5 AlmarM

AlmarM

    Programming my way.

  • Active Members
  • PipPipPipPipPipPip
  • 1,690 posts

Posted 02 September 2010 - 02:47 PM

Remade the whole function.
See first post for more information! ;)

Minesweeper

A minesweeper game created in autoit, source available.

 

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

 

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.


#6 wakillon

wakillon

    Tiny Tools Coder

  • Active Members
  • PipPipPipPipPipPip
  • 2,780 posts

Posted 02 September 2010 - 07:03 PM

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

       AutoIt Version : 3.3.10.2 / Beta 3.3.11.4 SciTE 3.3.7 OS:WIN7/WIN8.1 CPU:X64 OS:X64


#7 AlmarM

AlmarM

    Programming my way.

  • Active Members
  • PipPipPipPipPipPip
  • 1,690 posts

Posted 03 September 2010 - 06:32 AM

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, 03 September 2010 - 06:34 AM.

Minesweeper

A minesweeper game created in autoit, source available.

 

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

 

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.


#8 wakillon

wakillon

    Tiny Tools Coder

  • Active Members
  • PipPipPipPipPipPip
  • 2,780 posts

Posted 03 September 2010 - 03:39 PM

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)

       AutoIt Version : 3.3.10.2 / Beta 3.3.11.4 SciTE 3.3.7 OS:WIN7/WIN8.1 CPU:X64 OS:X64


#9 AlmarM

AlmarM

    Programming my way.

  • Active Members
  • PipPipPipPipPipPip
  • 1,690 posts

Posted 04 September 2010 - 07:12 AM

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, 04 September 2010 - 07:17 AM.

Minesweeper

A minesweeper game created in autoit, source available.

 

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

 

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.


#10 wakillon

wakillon

    Tiny Tools Coder

  • Active Members
  • PipPipPipPipPipPip
  • 2,780 posts

Posted 04 September 2010 - 09:07 AM

I'm on XP sp3 too Posted Image

       AutoIt Version : 3.3.10.2 / Beta 3.3.11.4 SciTE 3.3.7 OS:WIN7/WIN8.1 CPU:X64 OS:X64


#11 AlmarM

AlmarM

    Programming my way.

  • Active Members
  • PipPipPipPipPipPip
  • 1,690 posts

Posted 04 September 2010 - 10:37 AM

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.

Minesweeper

A minesweeper game created in autoit, source available.

 

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

 

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.


#12 wakillon

wakillon

    Tiny Tools Coder

  • Active Members
  • PipPipPipPipPipPip
  • 2,780 posts

Posted 04 September 2010 - 10:43 AM

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

       AutoIt Version : 3.3.10.2 / Beta 3.3.11.4 SciTE 3.3.7 OS:WIN7/WIN8.1 CPU:X64 OS:X64


#13 AlmarM

AlmarM

    Programming my way.

  • Active Members
  • PipPipPipPipPipPip
  • 1,690 posts

Posted 04 September 2010 - 10:55 AM

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. ;)

Minesweeper

A minesweeper game created in autoit, source available.

 

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

 

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.


#14 wakillon

wakillon

    Tiny Tools Coder

  • Active Members
  • PipPipPipPipPipPip
  • 2,780 posts

Posted 04 September 2010 - 11:09 AM

Thanks , works fine now ! Posted Image

Edited by wakillon, 04 September 2010 - 11:10 AM.

       AutoIt Version : 3.3.10.2 / Beta 3.3.11.4 SciTE 3.3.7 OS:WIN7/WIN8.1 CPU:X64 OS:X64





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users