Jump to content

_FindStringInFiles UDF


cppman
 Share

Recommended Posts

Hi. I thought this UDF might be kind of useful for some people.. What it does is, it will search within a file or the filename for the specified string you are looking for based on the parameters you pass to it. It returns an array of all the files found that match your search parameters.

You must include array.au3, and file.au3 in order for this to work.

Global Const $FS_MATCH_FILENAME     = 0x02
Global Const $FS_MATCH_FILEDATA     = 0x04
Global Const $FS_MATCH_DEFAULT      = 0x10

;===============================================================================
;
; Description:      Returns a list of files that contain a keyword based on the parameters passed.
; Parameter(s):     
;                   $szSearchString     - The string you are searching for.
;                   $szSearchDirectory  - The directory you want to search in.
;                   $szFileType         - The type of file you want to search for.. for all files this should be "*.*"
;                   $nFlags             - $FS_ flags that define how the search is performed.
;                                       - $FS_MATCH_FILENAME searches for the search string, only within the filename.
;                                       - $FS_MATCH_FILEDATA searches for the search string, only within the file contents.
;                                       - $FS_MATCH_DEFAULT  searches for the search string within both the file name and file content.
;                   $bCaseSensitive     - Whether or not you want the search to be case sensitive.
; Requirement(s):   file.au3, array.au3.
; Return Value(s):  
;                   Success: Returns a 1-dimensional array, where [0] represents the number of elements it contains.
;                   Failure: Returns -1, and sets @error to 1
; Author(s):        Chris Coale (chris95219)
; Note(s):          None.
;
;===============================================================================
Func _FindStringInFiles($szSearchString, $szSearchDirectory = @ScriptDir, $szFileType = "*.*", $nFlags = 0x10, $bCaseSensitive = false)
    $avFiles = _FileListToArray($szSearchDirectory, $szFileType, 1)
    Local $avResults[1]
    if (not (IsArray($avFiles))) Then
        SetError(1) ;No files were found
        return -1
    EndIf
    For $i = 1 to $avFiles[0]
        if ($nFlags and $FS_MATCH_FILENAME) Then
            if (StringInStr($avFiles[$i], $szSearchString, $bCaseSensitive)) Then
                _ArrayAdd($avResults, $avFiles[$i])
            EndIf
        Elseif ($nFlags and $FS_MATCH_FILEDATA) Then
            $File = FIleOpen($avFIles[$i], 0)
            if(StringInStr(FileRead($File), $szSearchString, $bCaseSensitive)) Then
                _ArrayAdd($avResults, $avFiles[$i])
            EndIf
            FileClose($File)
        Elseif ($nFlags and $FS_MATCH_DEFAULT) Then
            $File = FileOpen($avFiles[$i], 0)
            if ( (StringInStr(FileRead($File), $szSearchString, $bCaseSensitive)) or (StringInStr($avFiles[$i], $szSearchString, $bCaseSensitive))) Then
                _ArrayAdd($avResults, $avFiles[$i])
            EndIf
            FileClose($File)
        EndIf
    Next
    $avResults[0] = UBound($avResults)-1
    return $avResults
EndFunc
oÝ÷ Øw«z+ìZ^¡ø­ßÛ.±¨jëh×6
$avWinFiles = _FindStringInFiles("win", @WindowsDir, "*.*", $FS_MATCH_FILENAME)
_ArrayDisplay($avWinFiles, "List of files that has the word 'win' in it's name.")
Edited by chris95219
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...