函数类型: 自定义函数
函数说明:
获取指定目录及子目录中所有指定格式文件列表
代码如下:
// 查找所有文件及子目录中符合条件的文档
procedure FindFiles(APath, AFileName: string; var Files: TStringList);
var
FSearchRec, DSearchRec: TSearchRec;
FindResult: Integer;
subdirName: String;
function IsDirNotition(ADirName: string): Boolean;
begin
Result := (ADirName = '.') or (ADirName = '..');
end;
function getDirName(ADirName: string): string;
begin
if ADirName[Length(ADirName)] <> '\' then
Result := ADirName + '\'
else
Result := ADirName;
end;
begin
APath := getDirName(APath);
FindResult := FindFirst(APath + AFileName, faAnyFile, FSearchRec);
try
// 当前目录中查找
while FindResult = 0 do
begin
if IsDirNotition(FSearchRec.Name) = False then
Files.Add(APath + FSearchRec.Name);
FindResult := FindNext(FSearchRec);
end;
// 在子目录中查找
FindResult := FindFirst(APath + '*.*', faDirectory, DSearchRec);
while FindResult = 0 do
begin
if (DSearchRec.Attr = 16) and (IsDirNotition(DSearchRec.Name) = False)
then
begin
subdirName := getDirName(APath + DSearchRec.Name);
// ShowMessage(subdirName);
FindFiles(subdirName, AFileName, Files);
end;
FindResult := FindNext(DSearchRec)
end;
finally
FindClose(FSearchRec);
end;
end;
用法示例:
无
《学习大师原创文档,请勿转载,侵权必究》
|