• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 139
  • Last Modified:

Finding a file

I need a procedure that will search someone's ENTIRE hard drive for a specific file, starting from the root.  I do not want this search to be visible (detectable) to the end-user, beyond the fact that their hard drive is going crazy.  :)

If anyone has experience with doing this programmatically, please reply.  

I'm new to Delphi, and could probably figure this out given enough time, I'm sure, but if you have a procedure that does this just laying around, please help me out.  It's an easy 100 points.
0
Tom Knowlton
Asked:
Tom Knowlton
  • 5
  • 4
1 Solution
 
Tom KnowltonWeb developerAuthor Commented:
Edited text of question.
0
 
simonetCommented:
Why don't you want the user to know your application is looking for a file?

Alex
0
 
philipleighsCommented:
Hi,

This takes a starting folder, a string list for the result, a filename to search for (no path) and true/false for subfolders.

Cheers,
Phil.

procedure GetFileListing(Folder: string; Files: TStringList; FileName: string; IncludeSubFolders: Boolean);
  var sr: TSearchRec;
      Res: Integer;
  begin
    if Folder <> '' then
      if Folder[Length(Folder)] <> '\' then
        Folder := Folder + '\';
    Res := FindFirst(Folder + FileName, faAnyFile, sr);
    while Res = 0 do
      begin
        if (sr.Name <> '.') and (sr.Name <> '..') then
          begin
            if DirectoryExists(Folder + sr.Name) then
              begin
                if IncludeSubFolders then
                  GetFileListing(Folder + sr.Name, Files, FileName, IncludeSubFolders);
              end
            else
              begin
                if sr.Name = FileName then
                  begin
                    Files.Add(Folder + sr.Name);
                  end;
              end;
          end;
        Res := FindNext(sr);
      end;
    FindClose(sr);
  end;

0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
philipleighsCommented:
Hmm,

Maybe you will want to change this:

                if sr.Name = FileName then

to this:

                if LowerCase(sr.Name) = LowerCase(FileName) then

or use CompareText or something.

Cheers,
Phil.
0
 
Tom KnowltonWeb developerAuthor Commented:
Can you give me an example procedure call?

For example, if I want to start from the Root of my C drive, looking for file 'windows.ini' would I do this?:


....
....
//Set parameters
Folder := '\';  //Root directory???
Files.Add('windows.ini');
IncludeSubFolders := true;
....
....
....
//Here is the call...
GetFileListing(Folder, Files, FileName, IncludeSubFolders);
 
Is  \   a valid folder?  Remember I want to search the ENTIRE hard drive, potentially.

Thanks Phil!

PS:  I won't have time to try this out until tomorrow.

Tom
0
 
philipleighsCommented:
Sure, something like this. (BTW, the string list contains a list of the files that were found).

procedure TForm1.Button1Click(Sender: TObject);
var FoundFiles: TStringList;
    Index: Integer;
begin
  FoundFiles := TStringList.Create;
  try
    GetFileListing('c:\', FoundFiles, 'windows.ini', true);
    for Index := 0 to FoundFiles.Count - 1 do
      ShowMessage(FoundFiles[Index]);
  finally
    FoundFiles.Free;
  end;
end;


Whoops, I see a bug in the original code I posted. Change this line:

Res := FindFirst(Folder + FileName, faAnyFile, sr);

to:

Res := FindFirst(Folder + '*.*', faAnyFile, sr);

Cheers,
Phil.
0
 
Tom KnowltonWeb developerAuthor Commented:
Excellent work Phil.  Was this something you had ready-made, or figured-out on-the-spot?

Anyway, please resubmit your response above as an answer.  You get the points.
0
 
philipleighsCommented:
It was code I wrote a while ago, but it performed a different function. You would pass it some extensions in a string eg 'dll|ocx|exe' and it would add all the files that matched to the strings list. So I really only had to make some small changes to search for one file.

Glad you like it! and thanks for the points.

Cheers,
Phil.
0
 
philipleighsCommented:
Whoops, I forgot to click Answer!
0
 
Tom KnowltonWeb developerAuthor Commented:
Thanks for your answer, Phil.

Tom

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now