码迷,mamicode.com
首页 > 其他好文 > 详细

【跟随万一老师的足迹】查找目录下文件,在万一老师的基础上升级下,支持多文件查找 - 文件操作(一)

时间:2015-02-07 13:01:10      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

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

//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;

 

【跟随万一老师的足迹】查找目录下文件,在万一老师的基础上升级下,支持多文件查找 - 文件操作(一)

标签:

原文地址:http://www.cnblogs.com/jupt/p/4278592.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!