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.
QiscoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Eddie ShipmanAll-around developerCommented:
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
Eddie ShipmanAll-around developerCommented:
Change
FileMask := '*.bmp';
to
FileMask := '*.*';
0
Wim ten BrinkSelf-employed developerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

esoftbgCommented:
download an example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21195960.zip
0
Wim ten BrinkSelf-employed developerCommented:
@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 BrinkSelf-employed developerCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.