打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
delphi 获取一个文件夹下的所有文件

delphi 获取一个文件夹下的所有文件

作者:admin 来源: 日期:2014/1/22 22:41:13 人气:23837 标签:

 

获取一个文件夹下的所有文件
//不包括文件夹里面的文件
// 注意,path后面不要有要有'\';
//  Memo1.Lines := Searchfile('C:\Users\Admin\Desktop\名人格言\Win32\Release\Data');
function Searchfile(path: string): TStringList;
var
  SearchRec: TSearchRec;
  found: integer;
begin
  Result := TStringList.Create;
  found := FindFirst(path + '\' + '*.*', faAnyFile, SearchRec);
  while found = 0 do
  begin
    if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') and
      (SearchRec.Attr <> faDirectory) then
      Result.Add(SearchRec.Name);
    found := FindNext(SearchRec);
  end;
  FindClose(SearchRec);

end;

获取一个文件夹下的所有文件
//包括文件夹里面的文件
uses Masks;

// procedure TForm1.Button1Click(Sender: TObject);
// begin
// Memo1.Lines.Clear;
// GetFileListEx('Z:\', '*.*', Memo1.Lines, False);
// Caption:=IntToStr(Memo1.Lines.count);
// end;
//
// procedure TForm1.Button2Click(Sender: TObject);
// begin
//
// Memo1.Lines.Clear;
// GetFileListEx('Z:\', '*.cs',  Memo1.Lines, true);
/// /GetFileListEx('Z:\', '*.cs;*.txt',  Memo1.Lines, true);
// Caption:=IntToStr(Memo1.Lines.count);
// end;

// 遍历目录及子目录
procedure GetFileListEx(FilePath, ExtMask: string; FileList: TStrings;
  SubDirectory: Boolean = True);
  function Match(FileName: string; MaskList: TStrings): Boolean;
  var
    i: integer;
  begin
    Result := False;
    for i := 0 to MaskList.Count - 1 do
    begin
      if MatchesMask(FileName, MaskList[i]) then
      begin
        Result := True;
        break;
      end;
    end;
  end;

var
  FileRec: TSearchRec;
  MaskList: TStringList;
begin
  if DirectoryExists(FilePath) then
  begin
    if FilePath[Length(FilePath)] <> '\' then
      FilePath := FilePath + '\';
    if FindFirst(FilePath + '*.*', faAnyFile, FileRec) = 0 then
    begin
      MaskList := TStringList.Create;
      try
        ExtractStrings([';'], [], PChar(ExtMask), MaskList);
        FileList.BeginUpdate;
        repeat
          if ((FileRec.Attr and faDirectory) <> 0) and SubDirectory then
          begin
            if (FileRec.Name <> '.') and (FileRec.Name <> '..') then
              GetFileListEx(FilePath + FileRec.Name + '\', ExtMask, FileList);
          end
          else
          begin
            if Match(FilePath + FileRec.Name, MaskList) then
              FileList.Add( { FilePath + } FileRec.Name);
          end;
        until FindNext(FileRec) <> 0;
        FileList.EndUpdate;
      finally
        MaskList.Free;
      end;
    end;
    FindClose(FileRec);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.Lines.Clear;
  GetFileListEx('C:\Users\Admin\Desktop\名人格言\Win32\Release\Data', '*.txt',Memo1.Lines, False);
end;
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
用 TBytesStream 类实现的读文件为十六进制字符的函数
DELPHI 解析 JSON
Python For Delphi
输出用空格对齐字符串的函数
delphi快速获取网页源码方法
SpringMVC笔记(7):文件上传下载
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服