unit uGetFoldersAndFiles;
{< different methods to get files and folders from dir
}

interface

uses
  System.SysUtils, System.Classes, SYSTEM.IOUTILS,System.Types;

type
  TGetFoldersAndFiles = class(TDataModule)
  private
    procedure GetFoldersAndFilesToSDAAndSaveToDB(dir: string; var SDAResult: TStringDynArray);



    { Private declarations }



  public

    procedure GetFoldersAndFilesToSDA(dir:string; var SDAResult:TStringDynArray);

    procedure FileSearch(const dirName: string; Result:TStringList);

    procedure AddValueToSDA(var SDA:TStringDynArray;  Value:string);


    { Public declarations }
  end;

var
  GetFoldersAndFiles: TGetFoldersAndFiles;

implementation

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}


uses uDataBaseUnitServer;


{ TGetFoldersAndFiles }

procedure TGetFoldersAndFiles.AddValueToSDA(var SDA: TStringDynArray;  Value: string);
begin

  SetLength(SDA, Length(SDA)+1);
  SDA[Length(SDA)-1]:=Value;

end;






procedure TGetFoldersAndFiles.FileSearch(const dirName: string; Result:TStringList);
var
  searchResult: TSearchRec;
begin



  if FindFirst(dirName + '\*', faAnyFile, searchResult) = 0 then begin
    try
      repeat
        if (searchResult.Attr and faDirectory) = 0 then begin
          //if SameText(ExtractFileExt(searchResult.Name), '.gif') then begin
           // lbSearchResult.Items.Append(IncludeTrailingBackSlash(dirName) + searchResult.Name);

           Result.Add(IncludeTrailingBackSlash(dirName) + searchResult.Name);

         // end;
        end else

        if (searchResult.Name <> '.') and (searchResult.Name <> '..') then begin
          FileSearch(IncludeTrailingBackSlash(dirName) + searchResult.Name,Result);
        end;
      until FindNext(searchResult) <> 0
    finally
      FindClose(searchResult);
    end;
  end;
end;




procedure TGetFoldersAndFiles.GetFoldersAndFilesToSDAAndSaveToDB(dir: string; var SDAResult:TStringDynArray);

var SDAFiles:TStringDynArray; SDADirs:TStringDynArray;
  i: Integer;
  SearchOption: TSearchOption;

begin

// SearchOption:=TSearchOption.soAllDirectories;
// SDADirs:=TDirectory.GetDirectories(dir,'*.*',SearchOption);
// SDAFiles:=TDirectory.GetFiles(dir,'*.*',SearchOption);


AddValueToSDA(SDAResult,'   ');
AddValueToSDA(SDAResult,'---Dir---');
AddValueToSDA(SDAResult,dir);
AddValueToSDA(SDAResult,'---');


 SDADirs:=TDirectory.GetDirectories(dir);
 SDAFiles:=TDirectory.GetFiles(dir);

 AddValueToSDA(SDAResult,'   ');
 AddValueToSDA(SDAResult,'---SubDirs---');

 // do something with subdirs
 for i := Low(SDADirs) to High(SDADirs) do
 AddValueToSDA(SDAResult,SDADirs[i]);


 // do something with files
 AddValueToSDA(SDAResult,'   ');
 AddValueToSDA(SDAResult,'---Files---');
 for i := Low(SDAFiles) to High(SDAFiles) do
 AddValueToSDA(SDAResult,SDAFiles[i]);


 // Recursievely going on all subdirs...

  for i := Low(SDADirs) to High(SDADirs) do
  GetFoldersAndFilesToSDA(SDADirs[i], SDAResult);


end;




procedure TGetFoldersAndFiles.GetFoldersAndFilesToSDA(dir: string; var SDAResult:TStringDynArray);

var SDAFiles:TStringDynArray; SDADirs:TStringDynArray;
  i: Integer;
  SearchOption: TSearchOption;

begin

// SearchOption:=TSearchOption.soAllDirectories;
// SDADirs:=TDirectory.GetDirectories(dir,'*.*',SearchOption);
// SDAFiles:=TDirectory.GetFiles(dir,'*.*',SearchOption);


AddValueToSDA(SDAResult,'   ');
AddValueToSDA(SDAResult,'---Dir---');
AddValueToSDA(SDAResult,dir);
AddValueToSDA(SDAResult,'---');


 SDADirs:=TDirectory.GetDirectories(dir);
 SDAFiles:=TDirectory.GetFiles(dir);

 AddValueToSDA(SDAResult,'   ');
 AddValueToSDA(SDAResult,'---SubDirs---');

 // do something with subdirs
 for i := Low(SDADirs) to High(SDADirs) do
 AddValueToSDA(SDAResult,SDADirs[i]);


 // do something with files
 AddValueToSDA(SDAResult,'   ');
 AddValueToSDA(SDAResult,'---Files---');
 for i := Low(SDAFiles) to High(SDAFiles) do
 AddValueToSDA(SDAResult,SDAFiles[i]);


 // Recursievely going on all subdirs...

  for i := Low(SDADirs) to High(SDADirs) do
  GetFoldersAndFilesToSDA(SDADirs[i], SDAResult);


end;

end.
