Recent

Author Topic: file name to array  (Read 587 times)

pentilisea

  • Guest
file name to array
« on: March 18, 2023, 06:18:59 pm »
2 questions - sorry to keep you busy. I've got some pdf books
and a lot of short tutorials, but sometimes they do not contain
needed answers ...googling helps often but here i need some
advice...

1. Question
The playsound module is nice..

I use it with

Code: Pascal  [Select][+][-]
  1. playsound1.SoundFile:= filename ;
  2. playsound1.Execute;


However, I want the user to have the option to stop sound
within the application. Coulod not find a command like
playsound1.stop or playsound1,deactivate ....


2. Question
The project folder "training" has a subfolder perm and perm
has a subfolder "first-table".

first.table contains about 300 txt files with some aweful names
like: "Westspiel_Spielbank20220118_Tisch_13_AR.txt" - where
the number 20220118 is year 2022, 01 for January and 18 for the day.

Could shorten it with copy(filename, 19,27), but this is not
my question.

In order to process 1 file after theother automatically, I need
to load all 300 file names maybe into a string array.
So first file will be processed and when the process has ended,
next file will be processed.

Now I could write every file by hand into an array[0..299] but
rather would like to do this by code.
Then by load file array[0] - file would be loaded into an
ListBox and processed....

But how can I acces the first-table directory and get all its
file names identified and put into the array...???


Thanks,

Klaus








wp

  • Hero Member
  • *****
  • Posts: 11858
Re: file name to array
« Reply #1 on: March 18, 2023, 06:45:18 pm »
1) Look at the source code and you'll see that there is a StopSound method
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. begin
  3.   Playsound.StopSound;
  4. end;

2) The easiest way to list the file names in a folder is to call the FindAllFiles procedure in the FileUtil unit (https://lazarus-ccr.sourceforge.io/docs/lazutils/fileutil/findallfiles.html, https://wiki.freepascal.org/FindAllFiles). But note that the files names are not collected in an array but in a TStringList which you must create and destroy yourself; nevertheless, you can access the elements of the StringList in array-syntax with square brackets:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ProcessFolder(ADirectory: String);
  2. var
  3.   List: TStringList;
  4.   i: Integer;
  5.   filename: String;
  6. begin
  7.   List := TStringList.Create;
  8.   try
  9.     FindAllFiles(List, ADirectory, '*.txt', false);   // false ---> do not search subdirectories
  10.     for i := 0 to List.Count-1 do
  11.     begin
  12.       filename := List[i];  // is with specified directory
  13.       // do something with filename....
  14.     end;
  15.   finally
  16.     List.Free;
  17.   end;
  18. end;

[EDIT]
I corrected above code to avoid an seamingly endless search when ADirectory is 'c:\' and I only want the files in the directory itself. And it makes clear that it is not necessary to add the path to the found filenames.
« Last Edit: March 18, 2023, 07:03:46 pm by wp »

pentilisea

  • Guest
Re: file name to array
« Reply #2 on: March 18, 2023, 06:52:50 pm »
Thanks WP,
must have overlooked the StopSound .....

And thanks for the other stuff...

Klaus

P.S.
I corrected above code to avoid an seamingly endless search when ADirectory is 'c:\' and I only want the files in the directory itself. And it makes clear that it is not necessary to add the path to the found filenames.

No worry, I will play around and report ....
« Last Edit: March 18, 2023, 07:42:12 pm by pentilisea »

 

TinyPortal © 2005-2018