Solved

Find File

Posted on 2000-04-03
9
200 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
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!

 
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: 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.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 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