Forum > Beginners

file name to array

(1/1)

pentilisea:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---playsound1.SoundFile:= filename ;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:
1) Look at the source code and you'll see that there is a StopSound method

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button2Click(Sender: TObject);begin  Playsound.StopSound;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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.ProcessFolder(ADirectory: String);var  List: TStringList;  i: Integer;  filename: String;begin  List := TStringList.Create;  try    FindAllFiles(List, ADirectory, '*.txt', false);   // false ---> do not search subdirectories    for i := 0 to List.Count-1 do    begin      filename := List[i];  // is with specified directory      // do something with filename....    end;  finally    List.Free;  end;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.

pentilisea:
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 ....

Navigation

[0] Message Index

Go to full version