Jump to content

Search for a string in files


dolphins
 Share

Go to solution Solved by ioa747,

Recommended Posts

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

  • Solution

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

#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

  • Moderators

Hi,

There is also this UDF from guinness:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...