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 |1 Answer
Reset to default 2You 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;
版权声明:本文标题:delphi - Counting the number of files in a subdirectory within a directory with conditions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741616337a2388546.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
delphi iterate directory
– Panagiotis Kanavos Commented Feb 12 at 8:10