Showing posts with label delphi - FindNext(). Show all posts
Showing posts with label delphi - FindNext(). Show all posts

Friday, December 8, 2017

DELPHI - How enumerate all directories

Use faDirectory setting in FindFirst():
var
  recSearch : TSearchRec;
  sUNC : string;
...
begin
  { get unc path to base directory }

  sUnc := DB.QSetup_Globalupdate_unc.AsString;

  { enumerate only directories }

  i := FindFirst( sUNC + '*.*' , faDirectory, recSearch );
  while i = 0 do
    begin
      if recSearch.name[1] <> '.' then
        begin
          try
            { do something; } 
          except
          end;
        end;

      { try find next }
      i := FindNext( recSearch );
    end;
  FindClose( recSearch );
...
end;

Thursday, December 7, 2017

DELPHI - How enumerate all files in directory

This example enumerate all *.png files in directory (..and get its system date info).
var
  sr : TSearchRec;
  tim : TSystemTime;
  dDate : TDate;
  sMask : string;
begin
  ..
  { mask for .png files only }
  sMask := gcsDrawingDirectory + sFocusedValue + '*' + '.png';

  { find first file, to sr }
  i := FindFirst( sMask , faAnyFile, sr );
  while i = 0 do
    begin
      try
        { here is some action with file -> here get file time }       
 
        FileTimeToSystemTime( FileTime( sr.FindData.ftLastWriteTime) , tim );
        dDate := EncodeDate( tim.wYear, tim.wMonth, tim.wDay );

        { do some action..}

      except
      end;

  { try find next file }  
  i := FindNext( sr );
end;
{ close finding }
FindClose( sr );