Find file in all disks with given name and extension

Post your working scripts, libraries and tools.
Emanuele_Tinari[IT]
Posts: 5
Joined: 02 Dec 2023, 13:25
Contact:

Find file in all disks with given name and extension

Post by Emanuele_Tinari[IT] » 02 Dec 2023, 20:00

As I wrote in Stackoverflow https://stackoverflow.com/questions/77590673/autohotkey-v2-find-file-with-name-and-ext-in-all-drives here is my way to retrieve the complete path, filename and extension of a file with given name and extension.
Many examples here and on Google with Ahk v1 but nothing for v2.
Many examples start from A_ScriptDir or A_WorkingDir but... If I don't know where is but I know its name and ext? (Maybe a pdf downloaded in a different folder than Download for example...).
Here is my solution.

Code: Select all

FileToFind := "FILE_NAME.EXT"  ; The file to find
FileFoundedWithCompletePath := FindFileWithCompleteName(FileToFind)  ; Call Func

FindFileWithCompleteName(FileNameToFind) {
   Loop Parse DriveGetList() {                     ; 1st Loop: Into all Drives.
      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
            return FileNameToFind                  ; Return the file with its complete path.
         }
      }
   }
}
Now I ask to all experts here: Is it the best way to achieve it? Or there is a better (more performant for example) solutions? Tnx in advance.


[Mod edit: Removed empty Codebox tags.]

WKen
Posts: 204
Joined: 21 Feb 2023, 00:01

Re: Find file in all disks with given name and extension

Post by WKen » 03 Dec 2023, 05:47

For folder with more than one million files, the recursive loop script may crash. I only use recursion on third-level folders, hoping for a better solution.

Code: Select all

	Loop Files, path1 "\*.*", "FD"                                     ; Read items in a first-level source folder
	{
	    if (!dirExist(A_LoopFilePath)) {
		    destFile := strReplace(A_LoopFilePath, path1, path2)
			if fileExist(destFile)                                     ; If the file also exists on the target, synchronize ADS
		        if SyncADS(A_LoopFilePath, destFile)
				    syncList .= destFile "`n"
		} else {
		    Loop Files, A_LoopFilePath "\*.*", "FD"                    ; Read items in second-level source folders
			{
			    if (!dirExist(A_LoopFilePath)) {
			        destFile := strReplace(A_LoopFilePath, path1, path2)
				    if fileExist(destFile)                             ; If the file also exists on the target, synchronize ADS
				        if SyncADS(A_LoopFilePath, destFile)
						    syncList .= destFile "`n"
				} else {
			        Loop Files, A_LoopFilePath "\*.*", "R"             ; Recursively read files in third-level source folders
					{
						destFile := strReplace(A_LoopFilePath, path1, path2)
						if fileExist(destFile)
							if SyncADS(A_LoopFilePath, destFile)
							    syncList .= destFile "`n"
					}
				}
			}
		}
	}
Last edited by WKen on 09 Dec 2023, 08:28, edited 2 times in total.

Emanuele_Tinari[IT]
Posts: 5
Joined: 02 Dec 2023, 13:25
Contact:

Re: Find file in all disks with given name and extension

Post by Emanuele_Tinari[IT] » 03 Dec 2023, 07:07

WKen wrote:
03 Dec 2023, 05:47
For folder with more than one million files, the recursive loop script may crash. I only use recursion on third-level folders, hoping for a better solution.

Code: Select all

	Loop Files, path1 "\*.*", "FD"                                     ; Read items in a first-level source folder
	{
	    if (fileExist(A_LoopFilePath != "D")) {
		    destFile := strReplace(A_LoopFilePath, path1, path2)
			if fileExist(destFile)                                     ; If the file also exists on the target, synchronize ADS
		        if SyncADS(A_LoopFilePath, destFile)
				    syncList .= destFile "`n"
		} else {
		    Loop Files, A_LoopFilePath "\*.*", "FD"                    ; Read items in second-level source folders
			{
			    if (fileExist(A_LoopFilePath != "D")) {
			        destFile := strReplace(A_LoopFilePath, path1, path2)
				    if fileExist(destFile)                             ; If the file also exists on the target, synchronize ADS
				        if SyncADS(A_LoopFilePath, destFile)
						    syncList .= destFile "`n"
				} else {
			        Loop Files, A_LoopFilePath "\*.*", "R"             ; Recursively read files in third-level source folders
					{
						destFile := strReplace(A_LoopFilePath, path1, path2)
						if fileExist(destFile)
							if SyncADS(A_LoopFilePath, destFile)
							    syncList .= destFile "`n"
					}
				}
			}
		}
	}
Thank a lot @WKen for your tip. In this moment I don't have folder with more than 1M files, but I'll remember what You said in case of script crash. Have a good Sunday.

Post Reply

Return to “Scripts and Functions (v2)”