Mega Code Archive

 
Categories / Delphi / Files
 

Search an entire drive for a file QUICKLY

Title: Search an entire drive for a file QUICKLY Question: For a quick way to find a file try using the SearchTreeForFile function located in the library imagehlp.dll. I've loaded this dll explicitly - feel free to load it implicitly if you want to. Answer: //This function offers you a QUICK way of finding a single //file on any drive. lpStartingPoint is either a drive, or //a path, the search will begin at the starting point. The //file returned will be the first match found. //This is a good way to quickly find a file without asking //the user to specify the file's location. :-) //FUNCTION RESULTS: //The function will return the path and name of lpFileToFind. //If the lpFileToFind can't be found the return result is ''. //Any other problems will be returned in the form of a string function SearchForFileEx(lpStartingPoint, lpFileToFind: string): string; type TSearchTree = function(lpRootPath: PChar; lpInputPathName: PChar; lpOutputPath: PChar): integer; stdcall; var imagehlp_hwnd: THandle; SearchTreeNow: TSearchTree; lpResult: array [0..MAX_PATH + 256] of char; begin imagehlp_hwnd := LoadLibrary('imagehlp.dll'); if imagehlp_hwnd = 0 then Result := 'Failed to Load "imagehlp.dll"' else begin @SearchTreeNow := GetProcAddress(imagehlp_hwnd, 'SearchTreeForFile'); if @SearchTreeNow = nil then Result := 'Failed to find function "SearchTreeForFile"' else begin SearchTreeNow(PChar(lpStartingPoint), PChar(lpFileToFind), lpResult); Result := lpResult; end; end; FreeLibrary(imagehlp_hwnd); end; //Use the function like: procedure TForm1.Button1Click(Sender: TObject); begin showmessage(SearchForFileEx('C:\', 'winhlp32.hlp')); end; //Once you have found your file, you may like to check for version //numbers or some other way of verifying you have the right file..