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.
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tom KnowltonWeb developerAuthor Commented:
Thanks for your answer, Phil.

Tom

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.