【跟随万一老师的足迹】查找目录下文件,在万一老师的基础上升级

看了万一老师的“遍历某个文件的文件及子文件“,经过层层优化,结合实际需要,在万一老师代码的基础上,增加了多文件查找的功能

//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; IsFound: Boolean;begin S_Path := IncludeTrailingPathDelimiter(Trim(SourFile));//单元SysUtils中判断末尾是否包含文件夹路径符号'\&;,,没有的则补全 if not DirectoryExists(S_Path) then beginList.Clear;Exit; end; S_FileList := TStringList.Create; tryS_FileList.CommaText := FileName;TmpList := TStringList.Create;for I := 0 to S_FileList.Count – 1 dobeginif FindFirst(S_Path + S_FileList[I],faAnyFile,FileRec) = 0 thenrepeatif ((FileRec.Attr and faDirectory) <> 0) thenbeginif ((FileRec.Name <> '.') and (FileRec.Name <> '..')) thensysGetFileList(TmpList,IncludeTrailingPathDelimiter(S_Path + FileRec.Name),FileName);endelsebeginif ((FileRec.Attr and faDirectory) = 0) thenTmpList.Add(S_Path + FileRec.Name);end;until FindNext(FileRec) <> 0;end;FindClose(FileRec.FindHandle);if TmpList.CommaText <> '' then//空文件夹不添加路径beginif List.CommaText <> '' thenList.CommaText := List.CommaText + List.Delimiter + TmpList.CommaTextelseList.CommaText := TmpList.CommaText;end; finallyFreeAndNil(TmpList);FreeAndNil(S_FileList); end;end;

做对的事情比把事情做对重要。

【跟随万一老师的足迹】查找目录下文件,在万一老师的基础上升级

相关文章:

你感兴趣的文章:

标签云: