?
Solved

Find File

Posted on 1998-05-13
10
Medium Priority
?
172 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 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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

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…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

571 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