Solved

Programmatically finding a file on hard disk

Posted on 1998-02-24
9
276 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
Comment Utility
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
Comment Utility
Try to use a '\' as the directory name
0
 

Author Comment

by:aztec
Comment Utility
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
 

Author Comment

by:aztec
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:aztec
Comment Utility
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
Comment Utility
Do you want to use wildchards(*.pas) or just seeking asingle file?

Igor
0
 
LVL 5

Expert Comment

by:inter
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now