zoukankan      html  css  js  c++  java
  • 遍历目录及其子目录下面的某类型文件

    网上一大把这种方法,可惜许多不能用。这个我试过了,可以。

    USES Masks

    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;
    调用演示

    GetFileListEx(Edit1.Text,'*.pas;*.inc',pasFileList,true);

  • 相关阅读:
    ArrayList源码 (基于1.7)
    java.lang.Class类中的某些方法
    jdk1.8新特性 : 接口中可以有普通方法(非静态方法)和静态方法 , 颠覆了之前我的理解 : 接口中只能有共有常量和抽象方法的概念,后面必须要加一句jdk1.7和1..7之前
    Cesium中级教程10
    Cesium中级教程9
    Cesium中级教程8
    Cesium中级教程7
    Cesium中级教程6
    Cesium中级教程5
    Cesium中级教程4
  • 原文地址:https://www.cnblogs.com/hnxxcxg/p/2940605.html
Copyright © 2011-2022 走看看