zoukankan      html  css  js  c++  java
  • 获取一个文件夹下的所有文件

    获取一个文件夹下的所有文件
    //不包括文件夹里面的文件
    // 注意,path后面不要有要有'';
    //  Memo1.Lines := Searchfile('C:UsersAdminDesktop名人格言Win32ReleaseData');
    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:UsersAdminDesktop名人格言Win32ReleaseData', '*.txt',Memo1.Lines, False);
    end;
  • 相关阅读:
    hihocoder-1603-提取用户名
    hihocoder-1604-股票价格II
    求最长非重复子串
    程序员面试金典--取前K小的数
    hihocoder-1574-元素魔法--数学
    Struts2工作流程
    List和Map
    线程基本知识
    AOP通过反射机制实现动态代理/IOC依赖注入
    LayUi
  • 原文地址:https://www.cnblogs.com/xe2011/p/3484983.html
Copyright © 2011-2022 走看看