AutoHotkey_v2: Find file with name and ext in all drives

It’s hard to find a way to searching for a file in my PC with a given name and ext in AutoHotkey v2.
Many examples for v1.
After some time and searches, I build this Func I leave here for others.
I don’t have 1500 rep min to add new Tag, but this shoud be tagged as AutoHotkey_v2 to specify during searches: In Tags, only autohotkey is present with more than 4.6k questions!


    FileToFind := "FILENAME.EXT"                                         ; The file to find
    FileFoundedWithCompletePath := FindFileWithCompleteName(FileToFind)  ; Call Func passing the sting

    FindFileWithCompleteName(FileNameToFind) {
       Loop Parse DriveGetList() {                     ; 1st Loop: Into all Dives.
          PcDisks := (a_loopfield)                     ; Start w/first Drive found.
          Loop Files, PcDisks ":\*.*", "DFR" {         ; 2nd Loop: Recurse into all Files, Folders and Subfolders in Dive.
             if A_LoopFileName == FileNameToFind {     ; If find a file with same name and ext passed to the Func:
                FileNameToFind := A_LoopFileFullPath   ; fill Var with the full path, name and ext.
                return FileNameToFind                  ; return: FileFoundedWithCompletePath contains the file find with his complete path.
         }
      }
   }
}

Is it the best way to achieve this? Wait for an answer, tnx.

Leave a Comment