Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find a file somewhere on a drive!

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
Does anyone know the quickest programmatic way to search for a file on a drive without knowing which directory it is in? Clive [infinity]
 
hi Clive
Here's an example I stole from Delphi Magazine (issue 17). Apologises for large post.

procedure FindFile(initialPath : string;{ initial path}
fileMask :string; { mask to look For}
recursive: boolean;{ search subdirectories? }
stopOnFirstMatch: boolean; { one match?}
files: TStringList);{ add match(es) to list }

(* Starting at <initialPath>, FindFile will look for a match <fileMask>,if <Recursive> is True, all subdirectories beneath <initialPath> will be visited as well. If an initialPath is not given FindFile searches all non-removeable drives. Adds the paths where <fileMask> found to
<files> *)

type
TAryDrive = array[0..25] of char;
const
aryDrive: TAryDrive = ('a', 'b', 'c', 'd' ,'e', 'f', 'g', 'h','i', 'j', 'k', 'l', 'm', 'n', 'o', 'p','q', 'r', 's', 't', 'u', 'v', 'w', 'x','y', 'z');
var
currentPath, currentDrive: string;
i: byte;

function IsDriveValid(drive: integer): boolean;
{ returns true if a valid drive }
begin
{ not searching removable drives }
Result := not (GetDriveType(drive) = DRIVE_REMOVABLE);
if Result then begin
ChDir(Format('%s:', [aryDrive[drive]]));
Result := (IOResult = 0);
end;
end;

procedure SearchDirectory(fileMask: string; path: TFileName);
function MakePath(path, fileName: TFileName): TFileName;
function AddSlashIfNeeded(path: string): string;
begin
Result := Path;
if (not (path = '') and not (path[Length(path)] = '\')) then
Result := Format('%s\', [Result]);
end;
begin
Result := Concat(AddSlashIfNeeded(path), fileName);
writeln(Format('Path = %s', [Result]));
end;
var
searchRec: TSearchRec;
stopped: boolean;
begin
{ search the current directory for fileMask }
stopped := False;
try
if FindFirst(MakePath(path, fileMask), faAnyFile, searchRec) = 0
then
repeat
if searchRec.Attr <> faDirectory then begin
files.Add(ExtractFilePath(MakePath(Path,
searchRec.Name)));
if stopOnFirstMatch then
stopped := True
end
until (FindNext(searchRec) <> 0) or stopped;
finally
FindClose(searchRec);
end;

if recursive then
{search the subdirectories for fileMask }
try
{ Search current directory for subdirectories }
if FindFirst(MakePath(path, '*.*'), faDirectory, searchRec) = 0
then
repeat
with searchRec do
if (Name <> '.') and (Name <> '..') and (Attr =
faDirectory) then
SearchDirectory(fileMask, MakePath(path, Name));
{ we have to be gentle to the others apps }
Application.ProcessMessages;
until (FindNext(searchRec) <> 0) or stopped;
finally
FindClose(searchRec);
end;
end;
begin
if initialPath <> '' then
SearchDirectory(fileMask, initialPath)
else begin
{ bookmark current drive and directory }
GetDir(0, currentPath);
currentDrive := Copy(currentPath, 1, 1);

for i := 0 to High(aryDrive) do
if IsDriveValid(i) then
SearchDirectory(fileMask, Format('%s:\', [aryDrive]));

{ reset to previous path }
ChDir(Format('%s:', [currentDrive]));
ChDir(currentPath);
end;
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top