Solved

Find File

Posted on 2000-04-03
9
199 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

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

Suggested Solutions

Title # Comments Views Activity
Create Database on Android via Delphi dbExpress 3 110
URL for downloading Google Chrome for Win XP 2 182
Delphi inherited method 6 76
DBGrid or StringGrid ? 6 104
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 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