Solved

Find File

Posted on 1998-05-13
10
159 Views
Last Modified: 2010-04-06
Does anyone have a file find function?  I want it to look for a file and then return back to me the path information.  I also need something that can look at more than one drive...including network drives...Doesn't need to look at the CD or floppy. (maby as an option)
Thanks
0
Comment
Question by:Greedy
10 Comments
 
LVL 1

Accepted Solution

by:
ssite earned 50 total points
ID: 1345341
Doesn't delphi have a filesearch function ? Other than that, you should just write a recursive function to do that. If you want, I can e-mail you some sources.
0
 
LVL 1

Author Comment

by:Greedy
ID: 1345342
Yeah, that'll work but I need something that will give me all the valid drive letters too.
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1345343
Hi Greedy,

I'm not wanting ssite's points, but this is how you can get all the valid drive letters:

var
  LogicalDrives, i: Integer;
begin
  LogicalDrives := GetLogicalDrives;
  for i := 0 to 25 do
    if (LogicalDrives and (Round(Power(2, i))) > 0) then
      ShowMessage(Chr(65 + i) + ': is a valid drive.');
end;

(The Power function is defined in Delphi's Math unit.)

Cheers,
JB
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 1

Expert Comment

by:ssite
ID: 1345344
You can also just use this function to find out if the drive is ready for all 26 available letters

function DiskInDrive(Drive: Char): Boolean;
var
   ErrorMode: word;
begin
   if Drive in ['a'..'z'] then Dec(Drive, $20);
   if not (Drive in ['A'..'Z']) then
     raise EConvertError.Create('Not a valid drive ID');
   ErrorMode := SetErrorMode(SEM_FailCriticalErrors);
   try
    if DiskSize(Ord(Drive) - $40) = -1 then
      Result := False
    else
      Result := True;
    finally
      SetErrorMode(ErrorMode);
    end;
end;

0
 
LVL 1

Author Comment

by:Greedy
ID: 1345345
Is there an easy way to tell FileSearch to recurse the subdirectoris...Acctually this kinda sucks because I'm writing a ton of code to do this...I'm going to open the question back up to see if someone already has a unit built that I can use...if they don't and I have to write one myself I'll give it anyone that wants it and give you the points there ssite.  Right now I'm taking most of my code from the Filectrl unit to do the recurse and the drive type validation. (dtFixed, dtNetwork)
Thanks for the help  




0
 
LVL 1

Author Comment

by:Greedy
ID: 1345346
oh ssite...you said you have some source...can you send it to
flicky@frottage.com

0
 
LVL 1

Author Comment

by:Greedy
ID: 1345347
Not that I need it for what I'm doing but do you know how to have it look for wild cards too.  Like *.exe...I'll leave the question locked until I get a responce form you there ssite.

0
 
LVL 1

Expert Comment

by:ssite
ID: 1345348
Unfortunately, it's weekend here in israel and I'm away from my office machine. Just look up delphi's findfirst/findnext example, and use your wildcards there. The general code should check to see if the filename is not '.' or '..'  Then you should check the attribute to see if it's a directory. If so - recall the functions with the new dir specification. If it's a file - you found it.

Good Luck
0
 
LVL 1

Expert Comment

by:pjdb
ID: 1345349
This code determine the total size of the files of the directory (passed in the rep parameter) and recurse sub dir if recurse is true. You need to have "windows" in the uses clause after the "sysutils" because there is a double declaration for FindClose.

function det_taille(rep:string; recurse:boolean):longint;
var
  FileHandle:THandle;
  FindData:TWin32FindData;
begin
  rep:=rep + '\';
  result:=0;
  FileHandle:=FindFirstFile(pchar(rep + '*.*'), FindData);
  if FileHandle<>invalid_handle_value then
    repeat
      with FindData do
        if (dwFileAttributes and file_attribute_directory)=0 then
          result:=result + (nFileSizeHigh * maxdword) + nFileSizeLow
        else
          if recurse and (cFileName[0]<>#46) then
            result:=result + det_taille(rep + cFileName, recurse);
    until not findnextfile(FileHandle, FindData);
  findclose(FileHandle);
end;      {det_taille}

JDB
0
 
LVL 1

Author Comment

by:Greedy
ID: 1345350
I think I got it...anyone think they can make the code smaller...
Feal free to use this as you wish...most of it is taken from the filectrl.pas file...It's rather quick compaired to the start button's find file.  to test it I have a EditBox a Button a TAnimate (makes it pretty) and a ListBox.  THANKS Everyone for the help.  I'll give you the points, ssite, on monday so everyone can partake of the code witout haveing to pay 5 points.  

procedure TForm1.Button1Click(Sender: TObject);
var
  DriveNum, DirCounter, ListPointer : Integer;
  DriveChar : Char;
  DriveBits : set of 0..25;
  DirectoryList : TStringList;

  procedure ReadDirectoryNames(const ParentDirectory: string);
  var
    Status: Integer;
    SearchRec: TSearchRec;

    function SlashSep(const Path, S: String): String;
    begin
      if AnsiLastChar(Path)^ <> '\' then
        Result := Path + '\' + S
      else
        Result := Path + S;
    end;

  begin
    Status := FindFirst(SlashSep(ParentDirectory, '*.*'), faDirectory, SearchRec);
    try
      while Status = 0 do
      begin
        if (SearchRec.Attr and faDirectory = faDirectory) then
        begin
          if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
            DirectoryList.Add(SlashSep(ParentDirectory, SearchRec.Name));
        end;
        Status := FindNext(SearchRec);
      end;
    finally
      FindClose(SearchRec);
    end;
  end;

begin
  ListBox1.Items.Clear;
  Animate1.Visible := True;
  Animate1.Active := True;
  Integer(DriveBits) := GetLogicalDrives;
  For DriveNum := 0 to 25 do
  begin
    if not (DriveNum in DriveBits) then Continue;
    begin
      DriveChar := Char(DriveNum + Ord('a'));
      if FileCtrl.TDriveType(GetDriveType(PChar(DriveChar + ':\'))) in [FileCtrl.dtFixed, FileCtrl.dtNetwork] then
      begin
        DirectoryList := TStringList.Create;
        DirectoryList.Add(DriveChar + ':');
        ListPointer := 0;
        While ListPointer <= DirectoryList.Count-1 do
        begin
          ReadDirectoryNames(DirectoryList.Strings[ListPointer]);
          inc(ListPointer);
        end;
        For DirCounter := 1 to DirectoryList.Count -1 do
        begin
          if Length(FileSearch(Edit1.Text, (DirectoryList.Strings[DirCounter] + '\'))) <> 0 then
            ListBox1.Items.Add(DirectoryList.Strings[DirCounter] + '\' + Edit1.Text);
        end;
        DirectoryList.Free;
      end;
    end;
  end;
  ListBox1.Items.BeginUpDate;
  ListBox1.Sorted := True;
  ListBox1.Items.EndUpdate;
  Animate1.Active := False;
  Animate1.Visible := False;
end;

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 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