看了萬一老師的“遍歷某個文件的文件及子文件“,經過層層優化,結合實際需要,在萬一老師代碼的基礎上,增加了多文件查找的功能
//sysGetFileList(List,'c:\','*.doc,*.exe'); List通過查找添加多文件
//sysGetFileList(List,'c:\','*.doc'); List通過查找添加單文件
procedure sysGetFileList(List: TStrings; SourFile,FileName: string);
var
S_Path: String;
TmpList,S_FileList: TStringList;
FileRec,SubFileRec: TSearchRec;
I: Integer;
begin
S_Path := IncludeTrailingPathDelimiter(Trim(SourFile)); //單元SysUtils中判斷末尾是否包含文件夾路徑符號'\',沒有的則補全
if not DirectoryExists(S_Path) then
begin
List.Clear;
Exit;
end;
S_FileList := TStringList.Create;
try
S_FileList.CommaText := FileName;
TmpList := TStringList.Create;
for I := 0 to S_FileList.Count - 1 do
begin
if FindFirst(S_Path + S_FileList[I],faAnyFile,FileRec) = 0 then
repeat
if ((FileRec.Attr and faDirectory) <> 0) then
begin
if ((FileRec.Name <> '.') and (FileRec.Name <> '..')) then
sysGetFileList(TmpList,IncludeTrailingPathDelimiter(S_Path + FileRec.Name),FileName);
end
else
begin
if ((FileRec.Attr and faDirectory) = 0) then
TmpList.Add(S_Path + FileRec.Name);
end;
until FindNext(FileRec) <> 0;
end;
FindClose(FileRec.FindHandle);
if TmpList.CommaText <> '' then //空文件夾不添加路徑
begin
if List.CommaText <> '' then
List.CommaText := List.CommaText + List.Delimiter + TmpList.CommaText
else
List.CommaText := TmpList.CommaText;
end;
finally
FreeAndNil(TmpList);
FreeAndNil(S_FileList);
end;
end;