Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Programmatically finding a file on hard disk

Posted on 1998-02-24
9
Medium Priority
?
292 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

715 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