AlmarM Posted April 24, 2009 Share Posted April 24, 2009 (edited) EDIT (02-09-'10): I remade the whole function. See function header for more information. expandcollapse popup#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 September 4, 2010 by AlmarM 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. Link to comment Share on other sites More sharing options...
Valuater Posted April 24, 2009 Share Posted April 24, 2009 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) Link to comment Share on other sites More sharing options...
AlmarM Posted April 24, 2009 Author Share Posted April 24, 2009 (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! AlmarMEDIT: Updated! View first post! Edited April 24, 2009 by AlmarM 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. Link to comment Share on other sites More sharing options...
rajeshontheweb Posted April 25, 2009 Share Posted April 25, 2009 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.. Started late is much better than having never started it!!!!Failure is another step towards success. I've been messing around with: Adding Entry to 'Hosts'File Information Lister (Logger)Yet Another AutoIT Error Handler Yet Another AutoIT Error Handler & Debugger Control your App's TaskBar Button YCurrency Ticker (Latest Release : 16 Apr 2009)_WinInetInternetCheckConnection UDF Symantec Definitions Lister UDF _GetLocalIPAddresses UDF UDF to get Special Folder Information WMI_NetworkAdapterConfiguration2Array WMI_CDRomDriveCapabilities _ScriptExists - Check if your au3 script is running!! Uninstaller UDF Get Version for your application (at script level or compiled stage) Uninstaller Pro - faster alternative to windows application removal applet Link to comment Share on other sites More sharing options...
AlmarM Posted September 2, 2010 Author Share Posted September 2, 2010 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. Link to comment Share on other sites More sharing options...
wakillon Posted September 2, 2010 Share Posted September 2, 2010 EDIT (02-10-'10):_ArrayDisplay(_FileFindString($sFile, $sString)); Description......: Finds a certain string inside a FileChangeDirI 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" AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
AlmarM Posted September 3, 2010 Author Share Posted September 3, 2010 (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" 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 September 3, 2010 by AlmarM 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. Link to comment Share on other sites More sharing options...
wakillon Posted September 3, 2010 Share Posted September 3, 2010 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 $_Array =_FileFindString($sFile, $sString) _ArrayDisplay($_Array) AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
AlmarM Posted September 4, 2010 Author Share Posted September 4, 2010 (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 September 4, 2010 by AlmarM 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. Link to comment Share on other sites More sharing options...
wakillon Posted September 4, 2010 Share Posted September 4, 2010 I'm on XP sp3 too AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
AlmarM Posted September 4, 2010 Author Share Posted September 4, 2010 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. Link to comment Share on other sites More sharing options...
wakillon Posted September 4, 2010 Share Posted September 4, 2010 After adding Dim $iTimesFoundUBound because errorIt give me another error ! +>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 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts Link to comment Share on other sites More sharing options...
AlmarM Posted September 4, 2010 Author Share Posted September 4, 2010 After adding Dim $iTimesFoundUBound because error It give me another error ! +>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. Link to comment Share on other sites More sharing options...
wakillon Posted September 4, 2010 Share Posted September 4, 2010 (edited) Thanks , works fine now ! Edited September 4, 2010 by wakillon AutoIt 3.3.14.2 X86 - SciTE 3.6.0 - WIN 8.1 X64 - Other Example Scripts 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