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;

 

相关阅读 >>

Delphi中补齐字符串长度

Delphi 移除u盘的两种方法

Delphi 合并动态数组

Delphi 10.2 新特性之―tfdbatchmovejsonwriter

Delphi的listview自动排序

Delphi 遍历注册表

Delphi dbgrideh footer的设置和使用

Delphi timage保存图片到stream及从stream中取图片

Delphi winapi: getparent - 判断两个窗口是不是父子关系

Delphi idhttp1post上传图片

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...