How do I put all files in a directory into one variable?

I periodically have files that recorded into a directory and named by date and time of the recording. I would like to have an ext that plays all files in that directory. I’m thinking there should be a way of putting all files in a variable and then telling asterisk to play that variable when a particular ext is reached. I don’t know how to do that. My goal is to not have to manually add the name of every new file into a dial plan. All files will be .gsm

Thank you in advance for considering my problem.

A little bashed but

PLAYBACK(${SHELL(find /var/lib/asterisk/sounds/yourdirectory/ -name “*.gsm”| tr ‘\n’ ‘&’|sed ‘s/.gsm//g’):0:-1})

should work

1 Like

Thank you, kind sir. This worked perfectly. The only problem I had was that I spend about 45 mins tweaking the code because it would not work initially. After that I realized that the code that was in the email notification was different from what was on here. I’m not sure whether you posted the code that was emailed to me and then you edited it. At any rate, what is on the forum works. Thank you very much.

Now, if it is not too much trouble, please give me the English version of the part of the code I have below. I find it helpful to know what it is I am copying and pasting. Once again, thank you in advance.

| tr ‘\n’ ‘&’|sed ‘s/.gsm//g’):0:-1

The Asterisk command SHELL sends the argument to whatever is defined as /bin/sh (not necessarily bash, be wary!) within that environment :-

The | character is a “pipe” that will send the output of one command to the input of another

tr “translates” ‘\n’ is a “new line” which is the separator of what ls produces, ‘&’ is the separator needed by playback.

sed is a “streameditor” it will substitute (s) all the .gsm extensions from the filenames with a null string because PLAYBACK will choke on it if you include the extension.

the :0:-1 is a qualifier to print only the first through the penultimate characters produced by the VARIABLE and printed by ${VARIABLE:start:end}, in effect the last & produced by the tr command will be excluded as that will also make PLAYBACK bitch and whine.

It will barf if you have too many files as there is a limit on the string length returned by SHELL.

man tr
man sed

2 Likes

Thank you for the tutorial and for mentioning that I should be paying attention to the number of files because of the limit on string length. That is something I would not have done. What is that limit, by the way? Also, are there forum rules regarding marking an issue as resolved etc or de we just say, “Thank you and good bye.” …and abandon the thread?

Without looking at the source code I can’t answer the length thing right now, it is probably a little bit shorter than the string you send that breaks :slight_smile:

PLAYBACK is rooted to ${ASTVARLIB}/sounds so you can “sed out” that in your return from “find” (ASTVARLIB is usually /var/lib/asterisk/) to increase capacity, watch out for the initial / , you shouldn’t use it if you go that way. I will leave that as an exercise to the students.

Well, a question about netiquette :smile: that’s a new one!!

In the good old day one would edit the subject to include [RESOLVED] or some such but this is mostly ignored here. There is also a “like” button shaped like a heart if you benefited from your interaction here (luckily for me at least there is not a “dis-like” one :wink: )

Either one would, perhaps both, be an acceptable and polite “exit”

2 Likes