Solved

Find File

Posted on 2000-04-03
9
196 Views
Last Modified: 2010-04-04
How do I find a file? I want to search all hard drives for a file lets say its called 'temp.exe'. Also, how do I get a list of all hard drives?? Thanks ;] I also have a lot of points(1800) so a good anwser will help you out ;]
0
Comment
Question by:nafa2221
9 Comments
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2680688
First off, you are asking 2 questions. They should be listed as such. There is a good example of getting the drives mapped to your machine on my webpage...www.drdelphi.com. You can do a search on getDriveType or loook at the samples link.

Good luck!!
0
 

Author Comment

by:nafa2221
ID: 2680705
DrDelphi:

1) your site is not up
2) please post an example to get all drives
3) post a way to search for a file on a drive.
4) I can raise the points if you feel 200 points is not enough ;]
0
 
LVL 7

Expert Comment

by:ahalya
ID: 2680748
Here's the code to find all the drives in the system.  This uses the "GetLogicalDrives" API.  You can find a more nicer routine in Delphi's FileCtrl unit (..\Source\VCL\FileCtrl.PAS)

procedure TForm1.GetDriveList;

var
  DriveNum: Integer;
  DriveName: Char;
  DriveBits: set of 0..25;


begin
  Integer(DriveBits) := GetLogicalDrives;
  for DriveNum := 0 to 25 do
  begin
    if not (DriveNum in DriveBits) then Continue;
    DriveName := Char(DriveNum + Ord('a')) + ':\';
    ComboBox1.Items.Add(DriveName);
  end;
end;


You'll have to use FindFirst and FindNext functions recursively to find any file  from a subdir. i'll dig out some code that does recursive search and post it.
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 1

Expert Comment

by:JoeBooth
ID: 2680753


Here is some code posted by INTHE that does what you are looking for.  If you give any points, give them to INTHE since this is his solution to a similar question



Function TForm1.ScanDrive( root, filemask: String; hitlist: TStrings ): Boolean;
  Function ScanDirectory( Var path: String ): Boolean;
    Var
      SRec: TSearchRec;
      pathlen: Integer;
      res: Integer;
    Begin
      label1.caption := path;
      pathlen:= Length(path);
      { first pass, files }
      res := FindFirst( path+filemask, faAnyfile, SRec );
      If res = 0 Then
      try
        While res = 0 Do Begin
          hitlist.Add( path + SRec.Name );
          res := FindNext(SRec);
        End;
      finally
        FindClose(SRec)
      end;
      Application.ProcessMessages;
      Result := not (FScanAborted or Application.Terminated);
      If not Result Then Exit;
 
      {second pass, directories}
      res := FindFirst( path+'*.*', faDirectory, SRec );
      If res = 0 Then
      try
        While (res = 0) and Result Do Begin
          If ((Srec.Attr and faDirectory) = faDirectory) and
            (Srec.name[1] <> '.')
          Then Begin
            path := path + SRec.name + '\';
            Result := ScanDirectory( path );
            SetLength( path, pathlen );
          End;
          res := FindNext(SRec);
        End;
      finally
        FindClose(SRec)
      end;
    End;
Begin
  FScanAborted := False;
  Screen.Cursor := crHourglass;
  try
    Result := ScanDirectory(root);
  finally
    Screen.Cursor := crDefault
  end;
End;
 
procedure TForm1.Button2Click(Sender: TObject);
Var
  ch: Char;
  root: String;
Begin
  root := 'C:\';
  For ch := 'A' to 'Z' Do Begin
    root[1] := ch;
    Case GetDriveType( Pchar( root )) Of
      DRIVE_FIXED, DRIVE_REMOTE:
        If not ScanDrive( root, edit1.text, listbox1.items ) Then           Break;
    End;
  End;
end;
 
0
 

Author Comment

by:nafa2221
ID: 2680787
can you give me a example of how to use the findfile function?
0
 
LVL 7

Accepted Solution

by:
ahalya earned 200 total points
ID: 2680803
note that the "DriveName" variable in the above code is of type string, and not char:

You can use a routine similar to the following to search all subdirs: i couldn't dig out my old code, so i've just typed in some code.   Please note this is NOT tested and may need changes. but you'll get the idea:

function FindFile(FileName:String);

var SearchRec : TSearchRec;
    Result : integer;
    Attr   : integer;
    dir : string;

begin;
Attr := faAnyFile;
//first look for file in current dir
Result := FindFirst(FileName, Attr, SearchRec);
FindClose(SearchRec);
if Result = 0 then
  begin;
  ShowMessage('File found in dir' +  ExtractFilePath(FileName))
  // you may want to add the dir anme to a list/memo for later use:
  Memo1.Lines.Add(FileName);
  end
else //not found in dir.  so look in sub dirs:
  begin;
  dir   := ExtractFilePath(FileName);
  Attr := faDirectory;
  Result := FindFirst(dir + '*.*', Attr, SearchRec);
  while Result = 0 do
     begin;
     if SearchRec.Attr and faDirectory = faDirectory then
         begin;
         FindFile( SearchRec.Name + '\' +  ExtractFileName(FileName));
         end
     Result := FindNext(SearchRec);
    end;
  FindClose(SearchRec);
end;

if you call this function as  FindFile('C:\Temp.exe'); then it'll search all of C: drive.

0
 
LVL 7

Expert Comment

by:ahalya
ID: 2680808
oops, didn't see joe booth's comment when i posted my second one.
0
 

Author Comment

by:nafa2221
ID: 2680860
ahalya: your example says it needs a result or something..I really dont know why it doesnt work...
0
 
LVL 7

Expert Comment

by:ahalya
ID: 2680877
nafa2221,

my mistake. I made FindFile as a procedure and not a function.  So change the declaration to

procedure FindFile(FileName:string);

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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