[Last Call] Learn how to a build a cloud-first strategyRegister Now

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

How to show files information.. TListView question

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
goaman
Asked:
goaman
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Wim ten BrinkCommented:
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
 
goamanAuthor Commented:
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
 
DavidBirch2dotComCommented:
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
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:
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
 
goamanAuthor Commented:
Hi esoftbg,
This is exactly what I was looking for.... Any Idea how to show current file images too? :D
0
 
esoftbgCommented:
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
 
goamanAuthor Commented:
esoftbg,
Anyway...  thanx man... My problem is solved.. :)


ps: You deserve these points.. cuz gave me what I was looking for.. :)
0
 
Wim ten BrinkCommented:
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

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!

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