Solved

Programmatically finding a file on hard disk

Posted on 1998-02-24
9
280 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 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TVirtualStringTree  search using TEdit 7 121
error 1.1 400 Bad request idhttp delphi 18 87
Dev Express grid collapse 2 44
CheckListBox usage 3 58
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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