File size and "last modified"

Hello there,

I would like to be able to fill a listview with the name, size, and "last modified" date of all files in any given directory. Any idea how I can get those values without having to actually open the files one by one ?

hatecaplettters
hatecaplettersAsked:
Who is Participating?
 
kretzschmarConnect With a Mentor Commented:
hi all,

here my version as mix of all comments

unit lv_files_u;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListView1: TListView;
    procedure Button1Click(Sender: TObject);
  private
    procedure GetFiles(Path: String);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


procedure TForm1.GetFiles(Path: String);
var
  SRec: TSearchRec;
  Item : TListItem;
  FD : TFileTime;
  SD : TSystemTime;
begin
  Application.ProcessMessages;
  If FindFirst(Path+'*.*', faAnyFile, SRec) = 0 then
  begin
    Item := Listview1.Items.Add;
    Item.Caption := SRec.Name;
    Item.SubItems.Add(IntToStr(Srec.Size));
    FD := Srec.FindData.ftLastWriteTime;
    FileTimeToSystemTime(FD,SD);
    Item.SubItems.Add(DateTimeToStr(SystemTimeToDateTime(SD)));
    while FindNext(SRec) = 0 do
    begin
      Item := Listview1.Items.Add;
      Item.Caption := SRec.Name;
      Item.SubItems.Add(IntToStr(Srec.Size));
      FD := Srec.FindData.ftLastWriteTime;
      FileTimeToSystemTime(FD,SD);
      Item.SubItems.Add(DateTimeToStr(SystemTimeToDateTime(SD)));
    end;
  end;
  FindClose(Srec);
end;

procedure TForm1.Button1Click(Sender: TObject);
var LC : TListColumn;
begin
  ListView1.Items.Clear;
  ListView1.Columns.Clear;
  LC := ListView1.Columns.Add;
  LC.Caption := 'Name';
  LC := ListView1.Columns.Add;
  LC.Caption := 'Size';
  LC := ListView1.Columns.Add;
  LC.Caption := 'Last Modified';
  ListView1.ViewStyle := vsReport;
  GetFiles('c:\');  //or any dir
end;

end.

meikl ;-)
0
 
LischkeCommented:
Hi,

actually you won't come around to touch each file once but this can be very quick. Usually you are enumerating files in a directory using FindFIrst/FindNext/FindClose. The accompanying search record contains all data you need:
- file size
- short and long name
- attributes
- creation date (this and the following in FindData member)
- last access time
- last write time

If your files are spread over various  folders then just use FindFirst and FindClose for each file...

Ciao, Mike
0
 
philipleighsCommented:
Or use GetFileAttributesEx (Win32 API)
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
King_DavidCommented:
procedure TForm1.LoopFiles(const InPath: TFileName);
var
  Status: integer;
  SearchRec: TSearchRec;
begin
  Application.ProcessMessages;
  Status := FindFirst(InPath+'*.*', faAnyFile, SearchRec);
  while Status = 0 do
    begin
    ListBox1.Items.Add(SearchRec.Name + '  ' +
                       IntToStr(SearchRec.Size) + '  ' +
                       DateToStr(SearchRec.Time));
    Status := FindNext(SearchRec);
    end;
  FindClose(SearchRec);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  LoopFiles('c:\');
end;
0
 
scrapdogCommented:
if you have Delphi 4 (or maybe even 3), check out the VirtualListView source code in the Demos folder
0
 
hatecaplettersAuthor Commented:
hmm, what I'm looking for is a way to allow the user to browse and get the content listed of all folders on all drives (including network) without having to use a drivecombobox or similar, and I want to be able to program it without a lot of API calls.

hatecapletters

btw, datetostr(searchrec.time) won't work, it has to be datetostr(filedatetodatetime(searchrec.time)) for it to work porperly ;-)
0
All Courses

From novice to tech pro — start learning today.