Solved

Find File

Posted on 2000-04-03
9
189 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Expert Comment

by:JoeBooth
Comment Utility


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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

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

Accepted Solution

by:
ahalya earned 200 total points
Comment Utility
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
Comment Utility
oops, didn't see joe booth's comment when i posted my second one.
0
 

Author Comment

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

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

7 Experts available now in Live!

Get 1:1 Help Now