Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Programmatically finding a file on hard disk

Posted on 1998-02-24
9
Medium Priority
?
298 Views
Last Modified: 2010-04-04
Hi...I need to programmatically be able to scan my hrad drive(s) searching for a specific file name. I know there's a 'FileSearch' command, but this requires you to specify the directories to search in. I won't know ahead of time which directory this file is in so therefore won't have this info. Is there a command to scan all hard drives on a machine to look for a file name?

Cheers
   Shawn Halfpenny
   drumme59@sprint.ca
0
Comment
Question by:aztec
9 Comments
 

Expert Comment

by:KirkGray
ID: 1359755
There is no specific command, as far as I know.

Use Findfirst and Findnext, in a recursive function.  
That should do the trick.  

Cheers dude!
0
 
LVL 1

Accepted Solution

by:
aluiken earned 200 total points
ID: 1359756
Try to use a '\' as the directory name
0
 

Author Comment

by:aztec
ID: 1359757
Hi Aluiken...
  You mean using FileSearch ? Like this?

FoundIt := FileSearch('FINDME.TXT','\');

..What drive will that search? The current one? How do I search other drives? How will I KNOW what the other drives are?

Regards,
  Shawn Halfpenny
  drumme59@sprint.ca

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:aztec
ID: 1359758
Hello Aluiken...
   Tried your suggestion...didn't work. Couldn't find the file. I think just putting '\' means it will just look in whatever default directory you happen to be in and no further.

Shawn
0
 

Author Comment

by:aztec
ID: 1359759
Hi KirkGray...
   Your suggestion about using 'findfirst' and 'findnext' will not work either, I'm afraid. With both of those functions, you have to specify a directory to look in. Like I said in my original question...I want to search all directories on all hard drives.

Regards,
   Shawn
0
 
LVL 5

Expert Comment

by:inter
ID: 1359760
Do you want to use wildchards(*.pas) or just seeking asingle file?

Igor
0
 
LVL 5

Expert Comment

by:inter
ID: 1359761
Hello everybody, here is the code for you, (you may enhace it, but by now I have limited time). To test it add a button to an empty form. Link Button1Click with the following method. Add a ListBox also and run it! As an example it will find all the directories containing unit1.pas and put them into the listbox.

If you want to search all drives, you should enumare them and call one for each.(Compiler version Delphi 2.0-for 1.0 you should change the TSearchRec type as far as I can remember):

function FindFileEx(Path : String;const FileName:string; FileList:TStrings):boolean;
var
  SearchRec : TSearchRec;
  res       : integer;
begin
  Result := FileExists(Path+'\'+FileName);
  if Result then
    FileList.Add(Path)
  else
  begin
    res := FindFirst(Path+'\*.*', faDirectory, SearchRec);
    while res = 0 do
    begin
      if (SearchRec.Name = '.') or (SearchRec.Name = '..') then
      begin
        res := FindNext(SearchRec);
        continue;
      end;
      if SearchRec.Attr and faDirectory > 0 then
         Result := FindFileEx(Path+'\'+SearchRec.Name, FileName, FileList);
      res := FindNext(SearchRec);
      Application.ProcessMessages;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  FindFileEx('c:', 'unit1.pas', ListBox1.Items);
end;

Sincerely,
Igor
0
 

Expert Comment

by:Marcelito
ID: 1359762
I think that Igor give you a good answer, but I have a freeware component for this, I can send you by e-mail. Contact me at mluna@pbm.com.ar. Good Luck!!

PD:Also I have a component that obtain all drives in a computer
0
 

Author Comment

by:aztec
ID: 1359763
Please give this Grade to Marcelito. he sent me a component that was just right. Aluiken's answer did not work.

Regards,
   Shawn Halfpenny
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Screencast - Getting to Know the Pipeline
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question