Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to Fill a TListView with all files of a given directory along with the syste Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
31-Oct-02
Category
System
Language
Delphi 2.x
Views
125
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas 

How to fill a TListView with all files of a given directory along with the system 
icons

Answer:

Here's some code from a recent project. FileList is a TListView. ScanDir() is a 
function from our product - it's basically a procedure that fills a TStrings object 
with a list of files matching a mask. You can ignore the TDirInfo(Node.Data) stuff 
- it's a small class that holds info on each folder as displayed in a 
TTreeView.TTreeNode.

This routine builds a TListView that's pretty much like the right pane in Windows 
Explorer, in that it supports both the list and report view, and displays the file 
type, size, and modified date in columns in report view.
1   
2   {Gets files in a folder and displays them in the ListView}
3   
4   procedure TExplorer.GetFilesInFolder(Node: TTreeNode);
5   var
6     SL: TStringList;
7     i: Integer;
8     Dat: TDirInfo;
9     AllSel: Boolean;
10    NewItem: TListItem;
11    FI: TSHFileInfo;
12    Dt: TDateTime;
13    TypeDesc: string;
14  begin
15    if not Assigned(Node) then
16      Exit;
17    SL := TStringList.Create;
18    if Screen.Cursor <> crHourglass then
19      Screen.Cursor := crHourglass;
20    try
21      {Need easier access to Node.Data than TDirInfo(Node.Data) typecasts}
22  		{Grab a local reference to the pointer}
23  		Dat := TDirInfo(Node.Data);
24      {Get files in this folder, but don't include subfolder files}
25      ScanDir(Dat.FullPath, '*.*', SL, False);
26      SL.Sorted := True;
27      {See if this folder has already been fully selected. 
28  		If so, we don't need to add it to the Folders list or increment selection count
29  		or bytes}
30      AllSel := (Folders.IndexOf(Dat.FullPath) > -1) or (Dat.Status = dsFull);
31      {Remove stuff that was previously displayed}
32      FileList.Items.BeginUpdate;
33      FileList.Items.Clear;
34      {Is this an empty folder?}
35      if SL.Count = 0 then
36      begin
37        FileList.SmallImages := StateImages;
38        NewItem := FileList.Items.Add;
39        NewItem.Caption := ' No files ';
40        NewItem.ImageIndex := NoFilesIndex;
41        FileList.Enabled := False;
42        Exit;
43      end;
44      FileList.SmallImages := SysImages;
45      {We have files. Add each one to the ListView}
46      for i := 0 to SL.Count - 1 do
47      begin
48        {Create a new TListItem}
49        NewItem := FileList.Items.Add;
50        {Assign the filename portion}
51        NewItem.Caption := ExtractFileName(SL[i]);
52        FillChar(FI, SizeOf(TSHFileInfo), #0);
53        {Get the icon for display, as well as the file type, with one function call. 	
54  			Note the flags:
55        SHGFI_SMALLICON - we want the small icon
56        SHGFI_SYSICONINDEX - we want the index into the system imagelist
57        SHGFI_TYPENAME - we want the file type description if there is one}
58        SHGetFileInfo(PChar(SL[i]), 0, FI, SizeOf(FI), SHGFI_ICON or SHGFI_SMALLICON
59          or SHGFI_SYSICONINDEX or SHGFI_TYPENAME);
60        {The subitems are only displayed in the 'detail' view, but they have
61  		  to be there all the time. See if Windows knows what type file this is}
62        TypeDesc := FI.szTypeName;
63        if TypeDesc = '' then
64          {Windows doesn't know - handle like Explorer does}
65          TypeDesc := Upper(ExtractFileExt(SL[i])) + ' file';
66        {Delete the period if we need to}
67        if Length(TypeDesc) > 1 then
68        begin
69          if TypeDesc[1] = '.' then
70            Delete(TypeDesc, 1, 1);
71        end;
72        {Display the file type description}
73        NewItem.SubItems.Add(TypeDesc);
74        {Here's the 'Size' column ...}
75        NewItem.SubItems.Add(Comma([GetFileSize(SL[i])], False));
76        {Assign the system imagelist index to this item}
77        NewItem.ImageIndex := FI.iIcon;
78        {Grab the file's time and date stamp and convert to Delphi TDateTime}
79        Dt := FileDateToDateTime(FileAge(SL[i]));
80        {Add the date column}
81        NewItem.SubItems.Add(DateToStr(Dt));
82        {Add the time column}
83        NewItem.SubItems.Add(FormatDateTime('hh:nn:ss ampm', Dt));
84        {If folder was fully selected, or this file was selected in a 
85  			previous visit to this folder, check it}
86        if AllSel or (Files.IndexOf(SL[i]) > -1) then
87          NewItem.Checked := True;
88      end;
89      FileList.Enabled := True;
90    finally
91      SL.Free;
92      FileList.Items.EndUpdate;
93      if Screen.Cursor <> crDefault then
94        Screen.Cursor := crDefault;
95    end;
96  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC