Link to home
Start Free TrialLog in
Avatar of Qisco
Qisco

asked on

Listing all the files in a directory

I am  using delhpi 7 enterprise edition. I would like to be able to list all the files in a drive including those in the subdirectories. I would like to put this list on a listview component. Is there any one with an idea on how this can be achieved.
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Check the post here: http://www.delphipages.com/threads/thread.cfm?ID=58674&G=58668

{function reads all the BMP's and stores them in a TListView.}
Change
FileMask := '*.bmp';
to
FileMask := '*.*';
ASKER CERTIFIED SOLUTION
Avatar of Wim ten Brink
Wim ten Brink
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
download an example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21195960.zip
@esoftbg, My solution is more interesting, though. It doesn't use any recursion. And to add the files to a listview instead of a stringlist, all that needs to be changed is the "Files: TStringList" part into "Files: TListView".
And of course adding the files to the listview should be altered. Replace "Files.Add( Folders[ I ] + SearchRec.Name );" by the code that is required to put them in whatever way you like in your listview. Maybe you also want to put the file attributes, the timestamp and the filesize in the listview?

Oh, my code forgets to free Folders at the end right before the last "end;" A "Folders.Free" should be added right before that. But perhaps you can find some useful thing to do with that folderlist either... :-)
Avatar of Qisco
Qisco

ASKER

Sorry guys I was away for a while(gone to the bundus to see my folks). Let me check out your solutions then I should answer tommorrow.
PS though Workshop_alex seems to be very interesting.
Avatar of Qisco

ASKER

give the points to Workshop Alex
Qisco, if you want the points to be given to someone, you will have to select the answer and click on the Accept button on the right side. :-)
There's also an option to split the points if you think more than one answer is useful. Check out the EE help for more information.
best way to list all files in a directory is the below code, there are 2 ways, the easy way or the way which you can get the file icons as well.

uses
shellapi
=
procedure TForm1.Button1Click(Sender: TObject);
var
SR : TSearchRec;
begin
//change the
  if FindFirst( 'C:\'+'*.*',faAnyFile,SR ) = 0 then begin
    while FindNext( SR ) = 0 do begin

      with listview1.Items.Add do begin
        Caption := SR.Name;
        SubItems.Add(inttostr(sr.Size));
        SubItems.Add(inttostr(sr.Time));
      end;
    end;
  end;
   FindClose( SR );
end;


procedure LV_InsertFiles(strPath: string; ListView: TListView; ImageList: TImageList);
var
  i: Integer;
  Icon: TIcon;
  SearchRec: TSearchRec;
  ListItem: TListItem;
  FileInfo: SHFILEINFO;
begin
  // Create a temporary TIcon
  Icon := TIcon.Create;
  ListView.Items.BeginUpdate;
  try
    // search for the first file
    i := FindFirst(strPath + '*.*', faAnyFile, SearchRec);
    while i = 0 do
    begin
      with ListView do
      begin
        // On directories and volumes
        if ((SearchRec.Attr and FaDirectory <> FaDirectory) and
          (SearchRec.Attr and FaVolumeId <> FaVolumeID)) then
        begin
          ListItem := ListView.Items.Add;
          //Get The DisplayName
          SHGetFileInfo(PChar(strPath + SearchRec.Name), 0, FileInfo,
            SizeOf(FileInfo), SHGFI_DISPLAYNAME);
          Listitem.Caption := FileInfo.szDisplayName;
          // Get The TypeName
          SHGetFileInfo(PChar(strPath + SearchRec.Name), 0, FileInfo,
            SizeOf(FileInfo), SHGFI_TYPENAME);
          ListItem.SubItems.Add(FileInfo.szTypeName);
          //Get The Icon That Represents The File
          SHGetFileInfo(PChar(strPath + SearchRec.Name), 0, FileInfo,
            SizeOf(FileInfo), SHGFI_ICON or SHGFI_SMALLICON);
          icon.Handle := FileInfo.hIcon;
          ListItem.ImageIndex := ImageList.AddIcon(Icon);
          // Destroy the Icon
          DestroyIcon(FileInfo.hIcon);
        end;
      end;
      i := FindNext(SearchRec);
    end;
  finally
    Icon.Free;
    ListView.Items.EndUpdate;
  end;
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
  // Assign a Imagelist to the ListView
  ListView1.SmallImages := ImageList1;
  // Show Listview in Report Style and add 2 Columns
  ListView1.ViewStyle := vsReport;
  ListView1.Columns.Add;
  ListView1.Columns.Add;
  LV_InsertFiles('C:\', ListView1, ImageList1);
end;