Jump to content

Need a recursive "find file"


Recommended Posts

Hi

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

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.

Link to comment
Share on other sites

You can modify this to do what you need

Search ("C:\Windows","hal.dll");replace with your search directory and file required

Func Search($current,$toFind)
    If StringRight($current,1) = "\" then $current = StringTrimRight($current,1)
    Local $search = FileFindFirstFile($current & "\*.*")
    While 1
        Dim $file = FileFindNextFile($search)
        If @error Or StringLen($file) < 1 Then ExitLoop
        If Not StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then

            IF $file = $toFind then Msgbox(0,"File found", $current & "\" & $file); you could add it to an array or whatever else you wanted to do with it
                
        EndIf
        If StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
            Search($current & "\" & $file, $toFind)
            
        EndIf
        
    WEnd
    FileClose($search)

EndFunc
Edited by ChrisL
Link to comment
Share on other sites

  • 3 months later...

You can modify this to do what you need

Search ("C:\Windows","hal.dll");replace with your search directory and file required

Func Search($current,$toFind)
    If StringRight($current,1) = "\" then $current = StringTrimRight($current,1)
    Local $search = FileFindFirstFile($current & "\*.*")
    While 1
        Dim $file = FileFindNextFile($search)
        If @error Or StringLen($file) < 1 Then ExitLoop
        If Not StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then

            IF $file = $toFind then Msgbox(0,"File found", $current & "\" & $file); you could add it to an array or whatever else you wanted to do with it
                
        EndIf
        If StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
            Search($current & "\" & $file, $toFind)
            
        EndIf
        
    WEnd
    FileClose($search)

EndFunc
Your code worked perfectly...Thank you, it's exactly what I needed .
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...