Jump to content

Recursive file search


GEOSoft
 Share

Recommended Posts

This is a re-write of Larrys old recursive search function

Description: Recursivly search for files and folders.

Syntax: File_Search($szRoot, [$Szmask = "*.*"[, $nFlag = 1[, $nOcc = 0]]])

Parameters: $szRoot = the folder to start searching from

$Szmask = The file mask (Default *.* returns all)

$nFlag (Optional) Default is 7 Can be added as follows

1 = Recursive Search

2 = Path and Filename

4 = Sorted array

$nOcc (Optional) - 1 = Return only the first matching file, anything else = Return all matching files (Default)

Requirements: Array.au3 for _ArraySort function

Return Values: On Success - Returns an array of the file names

On Failure - Returns 0

Modifications: Changed return value to an array

Now excepts an array or a '|' separated list to use multiple file masks

Accepts flags to return full path and/or sort the array

Note If sorting is not used then you do not have to #include array.au3

Example: $var = _FileSearch("C:\", "*.vbs|*.js") will return a sorted array of all the vbs and js files (including path) on C:\ drive

#include <array.au3>
Func _FileSearch($szRoot, $Szmask = "*.*", $nFlag = 7, $nOcc = 0)
    Local $hArray = $Szmask, $iRec, $iPath, $iSort
    Switch $nFlag
        Case 1
            $iRec = 1
            $iPath = 0
            $iSort = 0
        Case 2
            $iRec = 0
            $iPath = 1
            $iSort = 0
        Case 3
            $iRec = 1
            $iPath = 1
            $iSort = 0
        Case 4
            $iRec = 0
            $iPath = 0
            $iSort = 1
        Case 5
            $iRec = 1
            $iPath = 0
            $iSort = 1
        Case 6
            $iRec = 0
            $iPath = 1
            $iSort = 1
        Case Else
            $iRec = 1
            $iPath = 1
            $iSort = 1
    EndSwitch
    If NOT IsArray($hArray) Then $hArray = StringSplit($hArray, '|')
    
    Local $Hfile = 0, $F_List = ''
    $szBuffer = ""
    $szReturn = ""
    $szPathlist = "*"
    For $I = 1 To Ubound($hArray)-1
        $szMask = $hArray[$I]
        If NOT StringInStr ($Szmask, "\") Then
            $szRoot &= '\'
        Else
            
            $iTrim = StringInStr ($Szmask, "\",0,-1)
            $szRoot &= StringLeft ($Szmask, $iTrim)
            $Szmask = StringTrimLeft ($Szmask, $iTrim)
            
        EndIf
        If $iRec = 0 Then
            $Hfile = FileFindFirstFile ($szRoot & $szMask)
            If $Hfile >= 0 Then
                $szBuffer = FileFindNextFile ($Hfile)
                
                While NOT @Error
                    If $iPath = 1 Then $szReturn &= $szRoot
                    If $szBuffer <> "." AND $szBuffer <> ".." Then $szReturn &= $szBuffer & "*"
                    $szBuffer = FileFindNextFile ($Hfile)
                Wend
                
                FileClose ($Hfile)
            EndIf
        Else
            
            While 1
                $Hfile = FileFindFirstFile ($szRoot & "*.*")
                If $Hfile >= 0 Then
                    $szBuffer = FileFindNextFile ($Hfile)
                    
                    While NOT @Error
                        If $szBuffer <> "." AND $szBuffer <> ".." AND StringInStr (FileGetAttrib ($szRoot & $szBuffer), "D") Then _
                                $szPathlist &= $szRoot & $szBuffer & "*"
                        $szBuffer = FileFindNextFile ($Hfile)
                    Wend
                    
                    FileClose ($Hfile)
                EndIf
                If StringInStr ($szReturn, $Szmask) > 0 AND $nOcc = 1 Then
                    $szRoot = ''
                    ExitLoop
                EndIf
                $Hfile = FileFindFirstFile ($szRoot & $szMask)
                If $Hfile >= 0 Then
                    $szBuffer = FileFindNextFile ($Hfile)
                    
                    While NOT @Error
                        If $iPath = 1 Then $szReturn &= $szRoot
                        If $szBuffer <> "." AND $szBuffer <> ".." Then $szReturn &= $szBuffer & "*"
                        $szBuffer = FileFindNextFile ($Hfile)
                    Wend
                    
                    FileClose ($Hfile)
                EndIf
                If $szPathlist == "*" Then ExitLoop
                $szPathlist = StringTrimLeft ($szPathlist, 1)
                $szRoot = StringLeft ($szPathlist, StringInStr ($szPathlist, "*") - 1) & "\"
                $szPathlist = StringTrimLeft ($szPathlist, StringInStr ($szPathlist, "*") - 1)
            Wend
            
        EndIf
    Next
    If $szReturn = "" Then
        Return 0
    Else
        $szReturn = StringReplace($szReturn,'\\', '\')
        $F_List = StringSplit (StringTrimRight ($szReturn, 1), "*")
        If $iSort = 1 Then _ArraySort($F_List)
        Return $F_List
    EndIf
EndFunc    ;<===> _FileSearch()

I know that the switch statement can be shortened. I just can't get my head around it today.

Enjoy!!

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • 2 weeks later...

Example: $var = _FileSearch("C:\", "*.vbs|*.js") will return a sorted array of all the vbs and js files (including path) on C:\ drive

Script is good but its not seraching as mentiond above. If we search one folder then its showing files of both type (*.vbs & *.js). But is we search whole C drive its only showing files of first search option that is *.vbs..

Any idea how to resolve this & will it work if we want to search more file types(*.mp3|*.avi|*.mpeg|*.dat|*.3gp)

Thx rahul

Link to comment
Share on other sites

  • Moderators

Script is good but its not seraching as mentiond above. If we search one folder then its showing files of both type (*.vbs & *.js). But is we search whole C drive its only showing files of first search option that is *.vbs..

Any idea how to resolve this & will it work if we want to search more file types(*.mp3|*.avi|*.mpeg|*.dat|*.3gp)

Thx rahul

_FileListToArrayEx()

You can use a semi-colon as the delimeter for the different extensions.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Script is good but its not seraching as mentiond above. If we search one folder then its showing files of both type (*.vbs & *.js). But is we search whole C drive its only showing files of first search option that is *.vbs..

Any idea how to resolve this & will it work if we want to search more file types(*.mp3|*.avi|*.mpeg|*.dat|*.3gp)

Thx rahul

Sorry but I just noticed your post now. I'll take another look at it.

Edit: Can you post the line that you used to call the function?

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

_FileListToArrayEx()

You can use a semi-colon as the delimeter for the different extensions.

Does _FileListToArray() return full path now? For me that's a MUST.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Moderators

Does _FileListToArray() return full path now? For me that's a MUST.

The Ex does that I did yesterday.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

The Ex does that I did yesterday.

Good to know.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • 3 years later...

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...