Solved

Programmatically finding a file on hard disk

Posted on 1998-02-24
9
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 50 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
Industry Leaders: 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

732 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