Solved

How to show files information.. TListView question

Posted on 2004-08-09
8
309 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now