Help with Playlist creation script

Hello. I am hoping to have an automated way to create Playlist files from Genre txt files I'll be making with my music collection. This is for use with my WD Live player, and is so that certain albums / artists can live in multiple genre directories when I browse to them from my WD Live player.

Here's an example of my directory structure:

/mnt/user/Music/+Acoustic Guitar/Kaki King/Kaki King - 2003 - Everybody Loves You/.Genre.txt
/mnt/user/Music/Beck/Beck - 2008 - Modern Guilt/.Genre.txt
/mnt/user/Music/Maroon 5/Maroon 5 - 2010 - Hands All Over/.Genre.txt
/mnt/user/Music/_Playlists/*.m3u

The goal is to take each directory with a .Genre.txt file (not all directories have this), add that root Folder to the appropriate playlist.m3u file/s based on genre/s in .Genre.txt using relative paths in the Playlist file. Then take each of those m3u files and alphabetize the contents of the m3u file based on the Folder Name that the Genre.txt file lives in. Here's what I mean:

Genre.txt contents:
Kaki King - 2003 - Everybody Loves You -> .Genre.txt
Acoustic Guitar
Rock
Beck - 2008 - Modern Guilt -> .Genre.txt
Alternative
Rock
Maroon 5 - 2010 - Hands All Over -> .Genre.txt
Alternative
Rock

The m3u files should look like this:

/mnt/user/Music/_Playlists/Acoustic Guitar.m3u

../+Acoustic Guitar/Kaki King/Kaki King - 2003 - Everybody Loves You/

/mnt/user/Music/_Playlists/Alternative.m3u

../Beck/Beck - 2008 - Modern Guilt/
../Maroon 5/Maroon 5 - 2010 - Hands All Over/

/mnt/user/Music/_Playlists/Rock.m3u

../Beck/Beck - 2008 - Modern Guilt/
../+Acoustic Guitar/Kaki King/Kaki King - 2003 - Everybody Loves You/
../Maroon 5/Maroon 5 - 2010 - Hands All Over/

I'm fine with the m3u files being completely deleted and recreated each time this script runs, as I expect that will be the easiest solution.

Is this easily possible using Bash? I know there are some GUI solutions to what I'm looking to do, but if I'm able to get a solution using the .Genre.txt files, it would be so much more elegant.

---------- Post updated at 06:04 PM ---------- Previous update was at 03:49 PM ----------

Alright, here's what I have so far.

MusicDIR=/mnt/user/Music/
PlaylistDIR=/mnt/user/Music/_Playlists/

find $PlaylistDIR -type f -name "*.m3u" -exec rm -f {} \;

find $MusicDIR -type d | while read FOLDER
do
        if ls -A "$FOLDER" | grep .Genre.txt > /dev/null
        then
			cat "${FOLDER}/.Genre.txt" | while read thisGENRE; do
				echo "$FOLDER" | sed 's/\/mnt\/user\/Music/../g' >> "${PlaylistDIR}${thisGENRE}".m3u
			done
        fi
done

This will create all of the m3u files I need. The next step will be alphabetizing the m3u files based on the Folder name.

Currently one of my m3u files looks like this:
/mnt/user/Music/_Playlists/Rock.m3u

../+Acoustic Guitar/Kaki King/Kaki King - 2003 - Everybody Loves You/
../Beck/Beck - 2008 - Modern Guilt/
../Maroon 5/Maroon 5 - 2010 - Hands All Over/

However I want it to look like this:
/mnt/user/Music/_Playlists/Rock.m3u

../Beck/Beck - 2008 - Modern Guilt/
../+Acoustic Guitar/Kaki King/Kaki King - 2003 - Everybody Loves You/
../Maroon 5/Maroon 5 - 2010 - Hands All Over/

I know the Sort command is likely what I need, but I'm not certain how to sort only based on the folder name. Any help would be greatly appreciated.

put the original folder together with the dir name together and sort base on the 2nd field.

sep="|"
while read d
do
  # get rid of the trailing /
  dd=${d%/}

  # print the orginal and dirname, separated by a "|" (hopefully no "|" in the folder name
  echo "$d$sep${dd##*/}"
done < /mnt/user/Music/_Playlists/Rock.m3u | sort -t"$sep" -k 2 | cut -d"$sep" -f1
1 Like

Thanks! I think this is almost what I need. I'm a little confused about what "while read d" does (I'm very much a bash noob who uses Google extensively to program). I'm hoping to add some lines to my code which will look for any m3u files in my $PlaylistDIR (the ones that my script just created - currently set to /mnt/user/Music/_Playlists/), and sort them all one by one using your code.

How would your code need to be altered for that to work?

while read d

is the same as your

while read FOLDER

In my code, I get the input from the redirection whereas your's is getting it from the pipe

Got it. Thanks for the help, it's all working now!