Jump to content
Sign in to follow this  
roman

Need a recursive "find file"

Recommended Posts

roman

Hi

I need a simple function that searches for a specific file in a given directory and ALL its subdirectories (recursive).

The filename i search for is given - i need no wildcards.

The function should return the path to this file or the files (if more than one is found).

I have browsed through help und example files in this forum but nothing found. :(

Is there a crack who can help me? :)

Thanks,

Roman.

Share this post


Link to post
Share on other sites
nobbe

; does recursive search 

    $a = _FileSearch("c:\test\" & "*." & "jpg", 1)
    
    If $a[0] > 0 Then
        ; all files
        For $i = 1 To $a[0]

                                

        Next
    EndIf






;--------------------------------------------
;
; search

Func _FileSearch($szMask, $nOption)
    $szRoot = ""
    $hFile = 0
    $szBuffer = ""
    $szReturn = ""
    $szPathList = "*"
    Dim $aNULL[1]

    If Not StringInStr($szMask, "\") Then
        $szRoot = @ScriptDir & "\"
    Else
        While StringInStr($szMask, "\")
            $szRoot = $szRoot & StringLeft($szMask, StringInStr($szMask, "\"))
            $szMask = StringTrimLeft($szMask, StringInStr($szMask, "\"))
        WEnd
    EndIf
    If $nOption = 0 Then
        _FileSearchUtil($szRoot, $szMask, $szReturn)
    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 = $szPathList & $szRoot & $szBuffer & "*"
                    $szBuffer = FileFindNextFile($hFile)
                WEnd
                FileClose($hFile)
            EndIf
            _FileSearchUtil($szRoot, $szMask, $szReturn)
            If $szPathList == "*" Then ExitLoop
            $szPathList = StringTrimLeft($szPathList, 1)
            $szRoot = StringLeft($szPathList, StringInStr($szPathList, "*") - 1) & "\"
            $szPathList = StringTrimLeft($szPathList, StringInStr($szPathList, "*") - 1)
        WEnd
    EndIf
    If $szReturn = "" Then
        $aNULL[0] = 0
        Return $aNULL
    Else
        Return StringSplit(StringTrimRight($szReturn, 1), "*")
    EndIf
EndFunc   ;==>_FileSearch

Func _FileSearchUtil(ByRef $ROOT, ByRef $MASK, ByRef $RETURN)
    $hFile = FileFindFirstFile($ROOT & $MASK)
    If $hFile >= 0 Then
        $szBuffer = FileFindNextFile($hFile)
        While Not @error
            If $szBuffer <> "." And $szBuffer <> ".." Then _
                    $RETURN = $RETURN & $ROOT & $szBuffer & "*"
            $szBuffer = FileFindNextFile($hFile)
        WEnd
        FileClose($hFile)
    EndIf
EndFunc   ;==>_FileSearchUtil

Share this post


Link to post
Share on other sites
xircon

I always use this dos command:

attrib /s myfile.ex > tmp.txt

Then parse the output file.

Simple, but effective.

Steve

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.