dolphins Posted November 10, 2023 Share Posted November 10, 2023 Hi! I wonder if there is a command / function or similar for this: I want to search for a string in all files in a directory. I took a look in the online help for version 3.3.8.1 (for several reasons I have to use this version), but I could not find something that fits. Regards dolphins Link to comment Share on other sites More sharing options...
Solution ioa747 Posted November 10, 2023 Solution Share Posted November 10, 2023 I modified a script to match what you want, so you have something to start with. because it stops at the first one it finds. What do you do if there is another file with the Search String ? what are you going to do with the file you found? I don't know if the version 3.3.8.1 covers it expandcollapse popup#include <FileConstants.au3> #include <File.au3> Local $sMyFife = _FindInAllFiles(@ScriptDir & "\kfc\", "This is it") Run('explorer.exe /select,' & $sMyFife) ;---------------------------------------------------------------------------------------- Func _FindInAllFiles($sSearchDir, $sSearchString) ;~ Local $sSearchDir = @ScriptDir & "\kfc\" ; <------------ Local $iPosition ; $FLTAR_FILES (1) - Return files only, $FLTAR_RECUR (1) - Search in all subfolders (unlimited recursion) $ArraySrtfiles = _FileListToArrayRec($sSearchDir, "*.*", $FLTAR_FILES, $FLTAR_RECUR) If Not IsArray($ArraySrtfiles) Then ConsoleWrite("! Invalid input path: " & $sSearchDir & @CRLF) Return Else For $x = 1 To $ArraySrtfiles[0] ;ConsoleWrite($sSearchDir & $ArraySrtfiles[$x]& @CRLF) $iPosition = _FindStringInFile($sSearchDir & $ArraySrtfiles[$x], $sSearchString) If $iPosition > 0 Then ConsoleWrite($sSearchString & " found in file: " & $sSearchDir & $ArraySrtfiles[$x] & " at position:" & $iPosition & @CRLF) Return $sSearchDir & $ArraySrtfiles[$x] EndIf Next EndIf EndFunc ;==>_FindInAllFiles ;---------------------------------------------------------------------------------------- Func _FindStringInFile($sFilePath, $sSearchString) ; Open the file for reading and store the handle to a variable. Local $hFileOpen = FileOpen($sFilePath, $FO_READ) If $hFileOpen = -1 Then ConsoleWrite("! An error occurred when reading the file:" & $sFilePath) Return False EndIf ; Read the contents of the file using the handle returned by FileOpen. Local $sFileRead = FileRead($hFileOpen) Local $iPosition = StringInStr($sFileRead, $sSearchString) Return $iPosition EndFunc ;==>_FindStringInFile ;---------------------------------------------------------------------------------------- I know that I know nothing Link to comment Share on other sites More sharing options...
dolphins Posted November 10, 2023 Author Share Posted November 10, 2023 Thank you very much, ioa747! Link to comment Share on other sites More sharing options...
quickbeam Posted November 13, 2023 Share Posted November 13, 2023 (edited) If this is all the program does, then I feel obliged to point out this can also be done with one call to grep at the command line, if you have access to Unix commands (such as installing msys). Edited November 13, 2023 by quickbeam Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 13, 2023 Moderators Share Posted November 13, 2023 Hi, There is also this UDF from guinness: M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
dolphins Posted November 15, 2023 Author Share Posted November 15, 2023 Hi folks, thanks for the input. Since I have to stick with 3.3.8.1 on Windows I used FileFindFirstFile with wildcards and then FileFindNextFile and then I read the first line of the file to check for the string (I have the luck that my search string is always in the first line of those files) with StringInStr. Since that works fine, I will do it that way. Regards dolphins ioa747 1 Link to comment Share on other sites More sharing options...
rudi Posted November 17, 2023 Share Posted November 17, 2023 Hi. you could use an external call making use of powershell and process the result file: #include <File.au3> #include <Debug.au3> $SearchRoot='\\server\share\path' $SubString='my-string' $OutFile="C:\temp\test.txt" DirCreate ("C:\temp") FileDelete($OutFile) $Params="Get-ChildItem '" & $SearchRoot & "' -Recurse | Select-String '" & $SubString & "' -List | select path | % {$_.path} | out-file '" & $OutFile & "' -Encoding utf8" $result=ShellExecuteWait("powershell.exe","-executionpolicy remotesigned " & $Params,"","",@SW_HIDE) if $result > 0 Then MsgBox(0,"Search error","Search Root: " & $SearchRoot & @CRLF & _ "SearchString: " & $SubString) ElseIf FileGetSize($OutFile) > 0 Then $aResult=FileReadToArray($OutFile) _DebugArrayDisplay($aResult) Else MsgBox(0,"No results","Search Root: " & $SearchRoot & @CRLF & _ "SearchString: " & $SubString & @CRLF & _ "No file contains the search string.") EndIf regards, Rudi. Earth is flat, pigs can fly, and Nuclear Power is SAFE! 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