Listing Files On Directory On a TListView

Nathan Campos picture Nathan Campos · Jan 7, 2010 · Viewed 10k times · Source

I'm building a program that needs to on Form_Create, populate a TListView called FileList, the directory that I want to populate is where the compiled program is + \Files, as I never used a TListView I want to know how to do this?

Answer

Rob Kennedy picture Rob Kennedy · Jan 7, 2010

There are multiple parts to your question. I'll provide an overview here. If you need help on any particular step, please post a more specific follow-up question.

  1. Determine what "where the compiled program is" refers to.

    To get the full path of the EXE file, call ParamStr(0). To remove the EXE file name from that string, so you have just the directory portion, call ExtractFilePath. Make sure it ends with a backslash (IncludeTrailingPathDelimiter) and then append your "Files" directory.

  2. Get a list of files.

    Use FindFirst and FindNext to make a loop that looks at all the files. The names will include the "." and ".." relative directory names, so you may wish to exclude them. Beware that the files are not enumerated in any particular order. If you need them sorted alphabetically, for instance, you'll need to do that yourself. (They may appear to be in alphabetical order in your tests, but that won't always be true.)

    var
      Rec: TSearchRec;
    begin
      if FindFirst(path + '\*', faAnyFile, Rec) = 0 then try
        repeat
          if (Rec.Name = '.') or (Rec.Name = '..') then
            continue;
          if (Rec.Attr and faVolumeID) = faVolumeID then
            continue; // nothing useful to do with volume IDs
          if (Rec.Attr and faHidden) = faHidden then
            continue; // honor the OS "hidden" setting
          if (Rec.Attr and faDirectory) = faDirectory then
            ; // This is a directory. Might want to do something special.
          DoSomethingWithFile(Rec.Name);
        until FindNext(Rec) <> 0;
      finally
        SysUtils.FindClose(Rec);
      end;
    end;
    
  3. Add nodes to the control to represent the files.

    You might wish to do this in the hypothetical DoSomethingWithFile function I mentioned above. Use the list view's Items property to do things with the items, such as add new ones.

    var
      Item: TListItem;
    begin
      Item := ListView.Items.Add;
      Item.Caption := FileName;
    end;
    

    TListItem has additional properties; check the documentation for details. The SubItems property is useful if you're showing the list view in "report" mode, where there can be multiple columns for a single node.

  4. Associate images with the items.

    Nodes' images in a list view come from the associated image lists, LargeImages and SmallImages. They refer to one or more TImageList components on your form. Put your icon images there, and then assign the items' ImageIndex properties to the corresponding numbers.

Depending on how elaborate you want your program to be, you may wish to use Delphi's TShellListView control instead of doing all the above work yourself.