gius Posted January 31, 2015 Share Posted January 31, 2015 Hello everyone,I'm using this example '?do=embed' frameborder='0' data-embedContent>> in my example I search for the word "test" within each file in the folder: #include <Array.au3> #include <Constants.au3> Example() Func Example() Local $aArray = _FindInFile('test', @ScriptDir) _ArrayDisplay($aArray) EndFunc I would like to ask if I cansearch for the word "test" and have as the result of _ArrayDisplaythe entire string in which is written the word "test"? thank you Link to comment Share on other sites More sharing options...
water Posted January 31, 2015 Share Posted January 31, 2015 The function uses the Windows tool FindStr. There is parameter "/X" which "Print lines that match exactly.". So you would need to create a customized version of _FindInFile. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
gius Posted January 31, 2015 Author Share Posted January 31, 2015 (edited) Thanks Water for your reply,I can not find the parameter which "Print lines That match exactly."You can help me? thank you Edited January 31, 2015 by gius Link to comment Share on other sites More sharing options...
mikell Posted January 31, 2015 Share Posted January 31, 2015 You could also use something like this #include <File.au3> #include <Array.au3> $folder = @ScriptDir $ext = ".au3" $word = "func" ; $var = FileSelectFolder("Choose", "", 0, "") ; If not @error Then $folder = $var $aFiles = _FileListToArrayRec($folder, "*" & $ext, 1, 0, 0, 2) $lines = "" For $i = 1 to $aFiles[0] $content = FileRead($aFiles[$i]) ; $res = StringRegExp($content, '(?im)(.*\b\Q' & $word & '\E\b.*)\R?', 3) $res = StringRegExp($content, '(?im)(.*\Q' & $word & '\E.*)\R?', 3) If IsArray($res) Then $lines &= $aFiles[$i] & @CRLF $lines &= _ArrayToString($res, @CRLF) & @CRLF $lines &= "-----------------------------------" & @CRLF EndIf Next ConsoleWrite($lines) Link to comment Share on other sites More sharing options...
water Posted January 31, 2015 Share Posted January 31, 2015 Thanks Water for your reply,I can not find the parameter which "Print lines That match exactly."You can help me? thank you It is not a parameter of _FindInFile but of FindStr which is used in _FindInFile. My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
gius Posted February 3, 2015 Author Share Posted February 3, 2015 Thanks mikell for your help. I have changedConsoleWrite ($lines)with_ArrayDisplay ($lines)but nothing is displayed,What I'm wrong?it is possible to add two different Path where to search? $folder = @ScriptDir and @MyDocumentsDir thank you Link to comment Share on other sites More sharing options...
mikell Posted February 3, 2015 Share Posted February 3, 2015 In the code $lines is a string, to display the results as an array you must first build the array from this string - StringSplit works nice for this For the rest the code is detailed enough to allow you to easily modify it so that it fits your needs #include <File.au3> #include <Array.au3> $folder = @ScriptDir $ext = ".au3" $word = "func" ; $var = FileSelectFolder("Choose", "", 0, "") ; If not @error Then $folder = $var $aFiles = _FileListToArrayRec($folder, "*" & $ext, 1, 0, 0, 2) ; list files $lines = "" For $i = 1 to $aFiles[0] $content = FileRead($aFiles[$i]) $res = StringRegExp($content, '(?im)(.*\b\Q' & $word & '\E\b.*)\R?', 3) ; if $word must be a lone word ; $res = StringRegExp($content, '(?im)(.*\Q' & $word & '\E.*)\R?', 3) ; if $word can be part of another word If IsArray($res) Then ; $lines &= $aFiles[$i] & @CRLF $lines &= _ArrayToString($res, @CRLF) & @CRLF ; $lines &= "-----------------------------------" & @CRLF EndIf Next ;ConsoleWrite($lines) $array = StringSplit($lines, @crlf, 1) _ArrayDisplay($array) Link to comment Share on other sites More sharing options...
gius Posted February 3, 2015 Author Share Posted February 3, 2015 thanks again mikell,it is possible to even resulted in the Path of the filein the next column? I believe it is the result of $lines &= $aFiles[$i] & @CRLF thank you Link to comment Share on other sites More sharing options...
mikell Posted February 3, 2015 Share Posted February 3, 2015 Ha, for this you have to build a 2D array and in this case the approach is different This code does all you want #include <File.au3> #include <Array.au3> Global $folders[2] = [@ScriptDir, @MyDocumentsDir] $ext = ".au3" $word = "func" Global $aResult[1000][2], $n = 0 For $k = 0 to UBound($folders)-1 ; loop through folders $aFiles = _FileListToArrayRec($folders[$k], "*" & $ext, 1, 0, 0, 2) ; list files If not @error Then For $i = 1 to $aFiles[0] ; loop through files $content = FileRead($aFiles[$i]) ; the following regex captures the full lines containing $word $res = StringRegExp($content, '(?im)(.*\b\Q' & $word & '\E\b.*)\R?', 3) ; if $word must be a lone word ; $res = StringRegExp($content, '(?im)(.*\Q' & $word & '\E.*)\R?', 3) ; if $word can be part of another word If IsArray($res) Then $aResult[$n][0] = $aFiles[$i] ; file path For $j = 0 to UBound($res)-1 $aResult[$n+$j][1] = $res[$j] ; lines Next $n += $j EndIf Next EndIf Next Redim $aResult[$n][2] _ArrayDisplay($aResult) Link to comment Share on other sites More sharing options...
gius Posted February 4, 2015 Author Share Posted February 4, 2015 Thanks again mikell.I changed the parameter of _FileListToArrayRec adding $FLTAR_RECUR (1) - Search in all subfoldersbecause I want to do the research text even in subfolders.After a few minutes I have this error:Error Allocating memoryI can fix this error?With another method I can have the search also to subfolders? thank you 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