There's three distinct things you need to do to accomplish this:
Building a M3U file in and of itself is a fairly simple operation - in their simplest form, with no extended information, an M3U file is just a newline-delimited list of files that should be played in a playlist. So to create a playlist, you would simply need to write all the files you want to play into a new file. Depending on the scripting or programming technology you're using to do this, there's usually a fairly simple library that will allow writing data to a file.
- Iterate over a directory structure
In order to parse through all your mp3 files, you will need to look through a structure of subdirectories in order to analyze every file. This is a textbook case for recursion, which involves code that calls itself, commonly to work through a tree structure such as a directory with it's subdirectories. Here's some pseudo-code of how moving through a directory structure would look:
function CheckDirectory(directory)
for each file in directory
// do something with the files
for each subdirectory in directory
CheckDirectory(directory) // this will run the same code for each subdirectory
Eventually, this code will move down each "branch" in the directory structure and check all files, regardless of how deep the directory structure is or where in the directory structure files are located.
- Identify what constitutes a "single"
MP3 tags, to my knowledge, don't have a field identifying them as a single, so there's no straightforward way to determine what is a single and what isn't. A couple approaches you could try:
- Consider any directory with only one MP3 in it to represent a single. This is the most straightforward approach, but there's a lot of cases where this will get false positives - maybe you only own one song from an album, or there's a stray file somewhere for some reason.
- Compare the "song name" in the MP3 file to the "album name". Somewhat more sophisticated - if you go down this route you will need to parse the MP3 tags, which you can probably find a library for depending on what language you are using for this. Also keep in mind there's a good chance of edge conditions where the "album name" for a single may not match the song name exactly. A simple idea in theory, but the edge cases may make it very complicated if 100% accuracy is important.
- Find some sort of online web service that informs you of whether a song title is a single. If this exists, awesome, but I've certainly never heard of it.
All in all, your realistic options are probably 1 and 2. 1 is simpler, and will produce some false positives (songs that aren't singles but your logic thinks they are). 2 is more complicated, and will produce some false negatives (songs that ARE singles that your logic doesn't recognize). It's really up to what suits you best.