吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1477|回复: 6
收起左侧

[求助] 遍历文件夹时,怎么判断是子文件夹,还是文件?

  [复制链接]
冥界3大法王 发表于 2021-9-25 17:37
@DEATHTOUCH
[Delphi] 纯文本查看 复制代码
uses IOUtils, Types;

const path = 'C:\P7';
//获取指定目录及嵌套目录下的所有文件与子目录
procedure TForm1.Button3Click(Sender: TObject);
var
  dfs: TStringDynArray;
  str: string;
begin
  dfs := TDirectory.GetFileSystemEntries(path', TSearchOption.soAllDirectories, nil);
  Memo1.Clear;
  for str in dfs do Memo1.Lines.Add(str);
end;


要是子文件夹,那就动态生成菜单时变成菜单项。
若是文件,则成为单击菜单项时读取打开的文件内容

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

似风而逝 发表于 2021-9-25 17:43
System.SysUtils.DirectoryExists

System.SysUtils.FileExists
DEATHTOUCH 发表于 2021-9-25 18:26
本帖最后由 DEATHTOUCH 于 2021-9-25 18:41 编辑

加一个 filter 函数,在里面做一些事情就行了
[Delphi] 纯文本查看 复制代码
procedure TForm1.Button1Click(Sender: TObject);
var
  dfs: tarray<string>;
  filter: tdirectory.TFilterPredicate;
begin
  filter := function(const path: string; const SearchRec: TSearchRec): Boolean
    begin
      if SearchRec.Attr = faDirectory then
      begin
        // 如果是文件夹的操作
      end else begin
        // 不是文件夹的操作,其中 完整文件名 = path+'\'+SearchRec.Name;
      end;
      Result := True; // 设置为 false 就不添加到数组里
    end;
  dfs := tdirectory.GetFileSystemEntries(path, TSearchOption.soAllDirectories, filter);
end;


或者在数组里遍历,用前面说的 DirectoryExists 和 FileExists

免费评分

参与人数 1吾爱币 +4 热心值 +1 收起 理由
冥界3大法王 + 4 + 1 谢谢@Thanks!

查看全部评分

iyeoman 发表于 2021-9-25 21:04
liuxia 发表于 2021-9-26 08:21
好像有专门的方法吧
头像被屏蔽
yulinsoft 发表于 2021-9-26 09:06
提示: 作者被禁止或删除 内容自动屏蔽
c03xp 发表于 2021-9-26 09:25
WIN32_FIND_DATA findData;

...

if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-4 07:01

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表