Solved

How to show files information.. TListView question

Posted on 2004-08-09
8
318 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi exception 7 68
error 1.1 400 Bad request idhttp delphi 18 148
Multiple image collision 13 80
Problem working with dynamic array - help 2 6
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

829 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