Recent

Author Topic: list of files in directory  (Read 563 times)

pentilisea

  • Guest
list of files in directory
« on: December 05, 2023, 04:33:41 pm »
Hi,
after processing a file I want to load automatically next file in sequence.

currendir is a string variable later holding path

allfiles is a string array to hold a list of files in that directory

so far I'm not able to define allfiles with all *txt files in currentdir - see my dumb code below

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button132Click(Sender: TObject);
  2.  
  3.  
  4. var
  5.   currentdir: string;
  6.  
  7.   allfiles: TStringArray;
  8.   i: Integer;
  9. begin
  10.   currentdir := 'C:\Users\newar\Documents\gambas-projects\TRAINING-XV-USER\perm\Tisch7';
  11.   [b]allfiles=//insert list of files???[/b]
  12.   for i := Low(allfiles) to High(allfiles) do
  13.   begin
  14.     if allfiles[i] = ParamStr(0) then
  15.     begin
  16.       if i < High(allfiles) then
  17.         ListBox1.Items.LoadFromFile((allfiles[i + 1]));
  18.       Break;
  19.     end;
  20.   end;
  21. end;                  

thanks for your suggestions ...

KLaus


dseligo

  • Hero Member
  • *****
  • Posts: 1449
Re: list of files in directory
« Reply #1 on: December 05, 2023, 05:31:50 pm »
You can use TSearchRec, something like this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button132Click(Sender: TObject);
  2. var currentdir: string;
  3.     allfiles: TStringArray = nil;
  4.     sr: TSearchRec;
  5.     i: Integer = 0;
  6. begin
  7.   currentdir := 'C:\Users\newar\Documents\gambas-projects\TRAINING-XV-USER\perm\Tisch7';
  8.  
  9.   If FindFirst(currentdir + '\*.txt', faAnyFile, sr) = 0 then
  10.   begin
  11.     Repeat
  12.       SetLength(allfiles, i + 1);
  13.       allfiles[i] := sr.Name;
  14.       inc(i);
  15.     Until FindNext(sr) <> 0;
  16.     FindClose(sr);
  17.   end;
  18.  
  19.   // rest of code ...

TRon

  • Hero Member
  • *****
  • Posts: 3797
Re: list of files in directory
« Reply #2 on: December 05, 2023, 05:51:40 pm »
allfiles is a string array to hold a list of files in that directory

so far I'm not able to define allfiles with all *txt files in currentdir - see my dumb code below
Let Lazarus do the hard work for you with FindAllFiles.

It is returning a stringlist instead of a stringarray but in practice it works similar. See here for an example how to use.
I do not have to remember anything anymore thanks to total-recall.

 

TinyPortal © 2005-2018