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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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
kretzschmarCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.