Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Find the number of files or the oldest file matching a file specFormat this article printer-friendly!Bookmark function is only available for registered users!
Answers questions like: Whats the oldest file matching c:\windows\*.dll
Product:
Delphi all versions
Category:
Files Operation
Skill Level:
Scoring:
Last Update:
02/25/2000
Search Keys:
delphi delphi3000 article borland vcl code-snippet wildcards oldest findfirst findnext
Times Scored:
3
Visits:
3650
Uploader: Den Bedard
Company: DTX Technologies, LLC
Reference: N/A
 
Question/Problem/Abstract:
How do I count the number of files matching c:\temp\*.dat?
What is the oldest file matching c:\windows\*rep.*?
Answer:



{This unit contains a form with:
   a Button
   an EditBox

If you type into the edit box a string
such as c:\windows\*.dll, then
click on the button - a message box
will appear that shows text like this:

The oldest file matching c:\windows\*.dll
is c:\windows\somefile.dll
}


unit oldestfileu;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

  function FileCount(FileSpec: string): longint;
  function GetOldestFile(FileSpec: string): string;

implementation

{$R *.DFM}


{This function can be used to return the name
of the oldest file which matches the FileSpec}
function GetOldestFile(FileSpec: string): string;
var
  SR: TSearchRec;
  DosError: integer;
  Oldest: string;
  OldestDTime: TDateTime;
begin
  {if there is more than one file that matches FileSpec
   then find the one with the oldest date}
  if FileCount(FileSpec)>1 then
  begin
    DosError := FindFirst(FileSpec, faAnyFile-faDirectory, SR);
    if DosError=0 then
    begin
      Oldest := ExtractFilePath(FileSpec)+SR.Name;
      OldestDTime := FileDateToDateTime(SR.Time);
      while FindNext(SR)=0 do
      begin
        if FileDateToDateTime(SR.Time)        begin
          Oldest := ExtractFilePath(FileSpec)+SR.Name;
          OldestDTime := FileDateToDateTime(SR.Time);
        end;
      end;
      Result := Oldest;
    end;
  end
  else {if there is only one file then return its name}
  begin
    DosError := FindFirst(FileSpec, faAnyFile, SR);
    if DosError=0 then
    begin
      Result := ExtractFilePath(FileSpec)+SR.Name;
    end
    else {if there are no matching files then return blank}
      Result := '';
  end;
end;

{This function can be used to return the number
of files in a directory which match the FileSpec}
function FileCount(FileSpec: string): longint;
var
  SR: TSearchRec;
  DosError: integer;
  c: longint;
begin
  c := 0;
  DosError := FindFirst(FileSpec, faAnyFile, SR);
  if DosError=0 then
  begin
    if (SR.Name<>'.') and (SR.Name<>'..') then inc(c);
    while FindNext(SR)=0 do
    begin
      if (SR.Name<>'.') and (SR.Name<>'..') then inc(c);
    end;
  end
  else
    c := 0;

  FileCount := c;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage('The oldest file matching '+Edit1.Text
              +' is: '+GetOldestFile(Edit1.Text));
end;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
correction in code
    Den Bedard (Nov 5 2000 5:14PM)

the line

if FileDateToDateTime(SR.Time)        begin
          
should be

if FileDateToDateTime(SR.Time)   begin


Thanks to Mark Palladino for pointing this out to me.          
Respond

RE: correction in code
Den Bedard (Nov 5 2000 5:24PM)

let me try again

if FileDateToDateTime(SR.Time)        begin

should be

if FileDateToDateTime(SR.Time)<OldestDTime then
begin

apparently the less than symbol is messing up how the code is displayed
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
E. Irigoyen
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)