[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

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.
0
Qisco
Asked:
Qisco
  • 3
  • 2
  • 2
  • +2
1 Solution
 
EddieShipmanCommented:
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.}
0
 
EddieShipmanCommented:
Change
FileMask := '*.bmp';
to
FileMask := '*.*';
0
 
Wim ten BrinkCommented:
First the function that quicly adds all files with foldername to a stringlist, including subfolders...

procedure EnumerateFolder( const Folder: string; Files: TStringList );
var
  I: Integer;
  SearchRec: TSearchRec;
  Folders: TStringList;
begin
  Folders := TStringList.Create;
  Folders.Add( IncludeTrailingBackslash( Folder ) );
  I := 0;
  while ( I < Folders.Count ) do begin
    if ( FindFirst( Folders[ I ] + '*.*', faAnyFile, SearchRec ) = 0 ) then begin
      repeat
        if ( ( SearchRec.Attr and faDirectory ) = faDirectory ) then begin
          if not ( ( SearchRec.Name = '.' ) or ( SearchRec.Name = '..' ) ) then begin
            Folders.Add( IncludeTrailingBackslash( Folders[ I ] + SearchRec.Name ) );
          end;
        end
        else begin
          Files.Add( Folders[ I ] + SearchRec.Name );
        end;
      until ( FindNext( SearchRec ) <> 0 );
      FindClose( SearchRec );
    end;
    Inc( I );
  end;
end;

Now, how to call it? Simple. The first parameter is the root folder that you want to have enumerated and the second parameter is the stringlist that will contain the result.

Now, if you want things to be added to a Listview then change the stringlist parameter by a listview parameter and add listview items to it, with whatever columns you'd like to see in it. (Data can be retrieved by the SearchRec variable.)

This function will build a list of folders and subfolders too, which might be useful. But basically it needs this list so it knows which folder is the next one to enumerate.

Other people might give a solution that uses recursion to enumerate folders. This method is a bit nicer and most likely faster too...

But also keep in mind that Delphi has a special listview and treeview for files available as component. Combined, these two allow you to walk through the whole tree of files, but the listview will only display files in the folder selected by the treeview. (If you have them connected to one another.)

To enumerate all files from e.g. the root of your C: drive, use:

var
  FileList: TStringList;
begin
  FileList := TStringList.Create;
  EnumerateFolder( 'C:\', FileList );
  // Do something with the data.
  FileList.Free;
end;
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!

 
esoftbgCommented:
download an example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21195960.zip
0
 
Wim ten BrinkCommented:
@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... :-)
0
 
QiscoAuthor Commented:
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.
0
 
QiscoAuthor Commented:
give the points to Workshop Alex
0
 
Wim ten BrinkCommented:
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.
0
 
SaLzCommented:
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;
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now