Here's another interesting option I considered. You can call Scandir which 
returns a record with an enumerator. You can then use this to drop right into 
the for loop. It doesn't allocate memory and you can break the loop to stop the 
iteration. The benefit is you can avoid costly memory allocations for large 
directories but I don't think it's a replacement for a function that returns an 
array of TStringList.

type  
        TDirectoryEnumerator = record
          path: string;
          count: integer;
                name: string;
                info: TSearchRec;
                function GetEnumerator: TDirectoryEnumerator;
                function MoveNext: boolean;  
                property Current: string read name;  
        end;

function TDirectoryEnumerator.GetEnumerator: TDirectoryEnumerator;
begin
        result := self;
end;

Function TDirectoryEnumerator.MoveNext: Boolean;  
begin  
  if count = 0 then
        begin
                if FindFirst(path, faAnyFile, info) = 0 then
                        begin
                                name := info.name;
                                inc(count);
                                result := true;
                        end
                else
                        result := false;
        end
  else if FindNext(info) <> 0 then
                begin
                        FindClose(info);
                        result := false;
                end
        else
                begin
                        name := info.name;
                        inc(count);
                        result := true;
                end;
end;  

function ScanDir(path: string): TDirectoryEnumerator;
begin
        if not DirectoryExists(path) then
                raise Exception.Create('Directory "'+path+'"" doesn''t exist');
        result.path := path+DirectorySeparator+'*';
        result.count := 0;
end;



Regards,
        Ryan Joseph

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to