delphi 查找目录下文件(多文件查找)  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 查找目录下文件(多文件查找)


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

 

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

 


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.033651113510132 seconds