admin管理员组

文章数量:1295330

I have a directory, it's the main directory (let's call it programs). In the main directory, I have many subdirectories (let's call them program1, program2, and so on). In every subdirectory, I have many files (all the files are images in JPEG format). From time to time, I put different files in the subdirectories (depends on where I have to put them).

My goal is to count the number of files I put in the subdirectories in a period of time. So, if I chose from date1 to date2, I have a result in a table tell me that my subdirectory program1 has 30 files, and my subdirectory program2 has 25 files, and so on.

Can anyone help me with that?

I have a directory, it's the main directory (let's call it programs). In the main directory, I have many subdirectories (let's call them program1, program2, and so on). In every subdirectory, I have many files (all the files are images in JPEG format). From time to time, I put different files in the subdirectories (depends on where I have to put them).

My goal is to count the number of files I put in the subdirectories in a period of time. So, if I chose from date1 to date2, I have a result in a table tell me that my subdirectory program1 has 30 files, and my subdirectory program2 has 25 files, and so on.

Can anyone help me with that?

Share Improve this question edited Feb 12 at 16:52 Remy Lebeau 598k36 gold badges503 silver badges848 bronze badges asked Feb 12 at 7:27 zaoui34zaoui34 135 bronze badges 1
  • If the real question is how to list all files, Delphi - how to get a list of all files of directory is a duplicate. I found it by googling for delphi iterate directory – Panagiotis Kanavos Commented Feb 12 at 8:10
Add a comment  | 

1 Answer 1

Reset to default 2

You can use Delphi's SysUtils.FindFirst()/FindNext() functions to iterate a directory's contents, eg:

uses
  ..., SysUtils;

function CountFilesInDirInDateRange(const ADir: string;
  const AStartDateTime, AEndDateTime: TDateTime): Integer;
var
  SR: TSearchRec;
  FileDT: TDateTime;
begin
  Result := 0;
  if FindFirst(IncludeTrailingPathDelimiter(ADir) + '*', faAnyFile, SR) = 0 then
  try
    repeat
      if (SR.Attr and faDirectory) = 0 then
      begin
        FileDT := FileDateToDateTime(SR.Time);
        if (FileDT >= AStartDateTime) and (FileDT < AEndDateTime) then
          Inc(Result);
      end;
    until FindNext(SR) <> 0;
  finally
    FindClose(SR);
  end;
end;

type
  TFileCount = record
    Dir: string;
    Count: Integer;
  end;
  TFileCounts = array of TFileCount;

function CountFilesInAllSubDirsInDateRange(const AMainDir: string;
  const AStartDateTime, AEndDateTime: TDateTime): TFileCounts;
var
  BasePath: string;
  SR: TSearchRec;
  SubDirs: TStringList;
  I: Integer;
begin
  Result := nil;
  BasePath := IncludeTrailingPathDelimiter(AMainDir);
  SubDirs := TStringList.Create;
  try
    if FindFirst(BasePath + '*', faAnyFile, SR) = 0 then
    try
      repeat
        if ((SR.Attr and faDirectory) <> 0) and
           (SR.Name <> '.') and (SR.Name <> '..') then
        begin
          SubDirs.Add(SR.Name);
        end;
      until FindNext(SR) <> 0;
    finally
      FindClose(SR);
    end;
    SetLength(Result, SubDirs.Count);
    for I := 0 to SubDirs.Count-1 do
    begin
      Result[I].Dir := SubDirs[I];
      Result[I].Count := CountFilesInDirInDateRange(BasePath + SubDirs[I], AStartDateTime, AEndDateTime);
    end;
  finally
    SubDirs.Free;
  end;
end;

本文标签: delphiCounting the number of files in a subdirectory within a directory with conditionsStack Overflow