Solved

How to show files information.. TListView question

Posted on 2004-08-09
8
316 Views
Last Modified: 2010-04-05
How to show files information[filename, file size, File created date and time, File Defalt application] from selected folder with TListView component as Report?

Also pls show me a example of a selected file..


Thanks in advance!
0
Comment
Question by:goaman
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11760939
FindFirst/FindNext/FindClose...

var
  SearchRec: TSearchRec;
begin
  if ( FindFirst( 'C:\*.*', faAnyfile - faDirectory, SearchRec ) = 0 ) then begin
    repeat
      // Do something with data in searchrec.
      Memo1.Lines.Add(Format( 'Name: %s, size: %d, Full name: %s.', [ SearchRec.Name, SearchRec.Size, SearchRec.FindData.cFileName ] ));
    until ( FindNext( SearchRec ) <> 0 );
    FindClose( SearchRec );
  end;
end;

But the filedate/time must still be converted from a Windows-specific format to Delphi's TDateTime functionality.
0
 

Author Comment

by:goaman
ID: 11761306
Hi Alex,
This is good example, working just fine... I want this to work in TListView component which shows these all infromation as report... in 3 or 4 columns........



thanx in advance
0
 
LVL 7

Expert Comment

by:DavidBirch2dotCom
ID: 11761356
why not use a Tstringgrid? its an easy way of getting columns

the code would be

    StringGrid1.RowCount:= StringGrid1.RowCount+1;
    StringGrid1.cells[0,StringGrid1.RowCount-1]:= SearchRec.Name;
   StringGrid1.cells[0,StringGrid1.RowCount-1]:= SearchRec.Size;
  // ect.......

David
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 12

Accepted Solution

by:
esoftbg earned 250 total points
ID: 11761416
Hi goaman, here is an example with TListView:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button_Scan: TButton;
    Edit_Count: TEdit;
    Edit_Path: TEdit;
    ListView: TListView;
    procedure FormCreate(Sender: TObject);
    procedure Button_ScanClick(Sender: TObject);
  private { Private declarations }
    Count_of_files: Integer;
    procedure Scan_Path(PathName: string);
  public  { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Count_of_files := 0;
end;

procedure TForm1.Scan_Path(PathName: string);
var
  Found:         Integer;
  Attr:          Integer;
  FName:         string;
  SearchRec:     TSearchRec;
var
  ListItem:      TListItem;
  S:             string;
  T:             string;
  ST:            TSystemTime;  // SystemTimeTypes
  FT:            TFileTime;    // FileTimeTypes
  DT:            TDateTime;
begin
  Attr := faDirectory;
  Found := FindFirst(PathName+'*.*', Attr, SearchRec);
  while (Found=0) do
  try
    if (((FileGetAttr(SearchRec.Name) and faArchive) > 0)
    or  ((FileGetAttr(SearchRec.Name) and faAnyFile) > 0)) then
      FName := PathName + SearchRec.Name;
    if FileExists(FName) then
    begin
      S := SearchRec.Name;
      //....
      ListItem := ListView.Items.Add;
      ListItem.Caption := S;
      T := IntToStr(SearchRec.Size);
      ListItem.SubItems.Add(T);
      FT := SearchRec.FindData.ftCreationTime;
      FileTimeToSystemTime(FT, ST);
      DT := SystemTimeToDateTime(ST);
      ListItem.SubItems.Add(FormatDateTime('yyyy/mm/dd hh:mm:ss', DT));
      //....
      Inc(Count_of_files);
    end;
  finally
    Found := FindNext(SearchRec);
  end;
  FindClose(SearchRec);
end;

procedure TForm1.Button_ScanClick(Sender: TObject);
var
  ListColumn:    TListColumn;
begin
  Edit_Count.Font.Color := clBlack;
  Application.ProcessMessages;
  if DirectoryExists(Edit_Path.Text) then
  try
    //....
    Application.ProcessMessages;
    //....
    ListView.Clear;
    ListView.Columns.Clear;
    //....
    ListColumn := ListView.Columns.Add;
    ListColumn.Caption := 'Filå Name';
    ListColumn.Width := 256;
    ListColumn := ListView.Columns.Add;
    ListColumn.Caption := 'Size';
    ListColumn.Width := 128;
    ListColumn := ListView.Columns.Add;
    ListColumn.Caption := 'Created';
    ListColumn.Width := 128;
    //....
    Scan_Path(Edit_Path.Text);
  finally
    Edit_Count.Text := IntToStr(Count_of_files) + ' file(s)';
    Edit_Count.Font.Color := clBlue;
    Count_of_files := 0;
  end;
end;

end.

//........

object Form1: TForm1
  Left = 224
  Top = 128
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = bsSingle
  Caption = 'Scan *.TXT files in given path'
  ClientHeight = 446
  ClientWidth = 632
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poDefaultPosOnly
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Button_Scan: TButton
    Left = 4
    Top = 4
    Width = 128
    Height = 25
    Caption = 'Scan Path'
    TabOrder = 0
    OnClick = Button_ScanClick
  end
  object Edit_Count: TEdit
    Left = 528
    Top = 8
    Width = 96
    Height = 17
    BorderStyle = bsNone
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = [fsBold]
    ParentFont = False
    ReadOnly = True
    TabOrder = 1
  end
  object Edit_Path: TEdit
    Left = 136
    Top = 6
    Width = 384
    Height = 21
    TabOrder = 2
    Text = 'C:\Temp\'
  end
  object ListView: TListView
    Left = 4
    Top = 32
    Width = 620
    Height = 400
    Columns = <>
    GridLines = True
    ReadOnly = True
    TabOrder = 3
    ViewStyle = vsReport
  end
end
0
 

Author Comment

by:goaman
ID: 11761457
Hi esoftbg,
This is exactly what I was looking for.... Any Idea how to show current file images too? :D
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 11761631
Hi goaman,
I know I don't deserve the points, because I can't show the current file images....
But the life is hard, I just can not do that for now....
Thanks for the Grade: A
Best Regards,
emil :D
0
 

Author Comment

by:goaman
ID: 11761713
esoftbg,
Anyway...  thanx man... My problem is solved.. :)


ps: You deserve these points.. cuz gave me what I was looking for.. :)
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11762361
Too bad you already answered the question. To retrieve the file icon, include the ShellAPI unit in your project and use:

Icon := ExtractAssociatedIcon( HInstance,  PathName + SearchRec.Name, Idx );

to retrieve the icon for the file. You then have a handle to an icon (which you will have to release sooner or later) that you can use to paint in your listview.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question