Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Find File

Posted on 2000-04-03
9
Medium Priority
?
217 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
[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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 600 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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

597 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