Jump to content
LaunchBox Community Forums

Batch file to Auto-Generate .m3us


Recommended Posts

Batch file to Auto-Generate .m3us

View File

The new m3u auto generator for Launchbox is a great tool, but it also got me wondering if I couldn't create m3u files for every one of my Sony Playstation titles (including single disk games) and would that work in Retroarch? Something just appeals to me about the uniform file extension on import in Launchbox, and of course, if I wanted to launch games outside of the Launchbox environment I'd need some static m3us. I also wanted to have m3us with relative paths to the cue files, for simplicity sake.

So I asked on Stack Overflow and, not long after, some genuis had posted a working batch script. I figured I'd share it. I also tested whether or not single cue m3us would work in Retroarch, and it turns out they do.  

Important:

Your games directory should look like this:

Main Directory/

   [Game Name]/

      [whatever name].cue 

e.g Sony Playstation/Chrono Cross/disk 1.cue, disk 2.cue

  • The bat file should be run from the main directory
  • The bat file will name the m3u after the directory the cue files are in and place it in that same directory
  • The bat file will create m3us in every subdirectory which holds at least 1 cue file (it doesn't single out multidisk games as it is now, so be aware of that)
  • The bat file should work with any system provided it has the above directory set up and utilizes cue files
  • The m3us list relative paths to the cue files, not absolute paths. 

So continuing the above example, I ran the file in my "Sony Playstation" directory, and within the "Chrono Cross" directory an m3u file was generated and listed relative paths to all the cue files in that directory and was automatically named "Chrono Cross.m3u" (after the folder name, not the cue file(s) names or anything else in the folder).

Lastly, the Stock Overflow user wrote a batch script for both NTFS and FAT hard drives. Apparently the code is different for each. I personally have not tested the FAT version as my drive is NTFS. If anyone wants to check the code, the user also left a thorough explanation  that is absolutely unreadable to me. :D See here: https://stackoverflow.com/questions/60049048/need-a-batch-file-to-create-and-name-m3us-in-subdirectories

Hope it helps!

 

Edit Update:

Rednecked_crake provided this information below, and it might be helpful.

"

Late, but I thought adding this line before the for loop is helpful.

@echo off
del /S *.m3u
for /R "%~dp0" %%I in (*.cue) do for %%J in ("%%~dpI.") do echo %%~nxI>>"%%~dpI%%~nxJ.m3u"

It deletes any old m3u files beforehand. I found that if you ran it twice, it would just add the same lines again and confuse things.

"

Remember a batch file is just a text file so to edit it you can just right click it and edit it in text.

 


 

Edited by fromlostdays
Clarity.
  • Like 1
Link to comment
Share on other sites

2 hours ago, The_Keeper86 said:

Can this also work with chds?

I gotta tell ya, I'm FAR from coding and/or programming savvy but I would venture a guess that it can. 

@echo off
for /R "%~dp0" %%I in (*.cue) do for %%J in ("%%~dpI.") do echo %%~nxI>>"%%~dpI%%~nxJ.m3u"

I would try just changing the (*.cue) to (*.chd)

@echo off
for /R "%~dp0" %%I in (*.chd) do for %%J in ("%%~dpI.") do echo %%~nxI>>"%%~dpI%%~nxJ.m3u"

FAT:

@echo off
for /F "eol=| delims=" %%I in ('dir "%~dp0*.cue" /A-D-H /B /ON /S 2^>nul') do for %%J in ("%%~dpI.") do echo %%~nxI>>"%%~dpI%%~nxJ.m3u"
@echo off
for /F "eol=| delims=" %%I in ('dir "%~dp0*.chd" /A-D-H /B /ON /S 2^>nul') do for %%J in ("%%~dpI.") do echo %%~nxI>>"%%~dpI%%~nxJ.m3u"

I haven't tested this, can't as I don't have any of my roms in CHD format. To be safe, create a directory and then plop a single subdirectory with one or more chds in it to test and run the modified bat in the main directory.

In case you're not familiar with .bat files, you can just open the ones you can download here with notepad or wordpad, make the edit, and save it (good practice, save it with a different name).

Let me know if it works!

 

Edited by fromlostdays
  • Like 1
Link to comment
Share on other sites

48 minutes ago, fromlostdays said:

I gotta tell ya, I'm FAR from coding and/or programming savvy but I would venture a guess that it can. 


@echo off
for /R "%~dp0" %%I in (*.cue) do for %%J in ("%%~dpI.") do echo %%~nxI>>"%%~dpI%%~nxJ.m3u"

I would try just changing the (*.cue) to (*.chd)


@echo off
for /R "%~dp0" %%I in (*.chd) do for %%J in ("%%~dpI.") do echo %%~nxI>>"%%~dpI%%~nxJ.m3u"

FAT:


@echo off
for /F "eol=| delims=" %%I in ('dir "%~dp0*.cue" /A-D-H /B /ON /S 2^>nul') do for %%J in ("%%~dpI.") do echo %%~nxI>>"%%~dpI%%~nxJ.m3u"

@echo off
for /F "eol=| delims=" %%I in ('dir "%~dp0*.chd" /A-D-H /B /ON /S 2^>nul') do for %%J in ("%%~dpI.") do echo %%~nxI>>"%%~dpI%%~nxJ.m3u"

I haven't tested this, can't as I don't have any of my roms in CHD format. To be safe, create a directory and then plop a single subdirectory with one or more chds in it to test and run the modified bat in the main directory.

In case you're not familiar with .bat files, you can just open the ones you can download here with notepad or wordpad, make the edit, and save it (good practice, save it with a different name).

Let me know if it works!

 

Thanks! Sadly it just creates a Sony Playstation.m3us file with a list of all my PS1 games in chd weird ha ha.

Link to comment
Share on other sites

The guy who wrote the code said something: 

Quote

The batch file should be stored in Main Directory. Otherwise %~dp0 referencing the drive and path of the batch file ending always with \ must be replaced by full path of Main Directory ending also with a backslash.

I don't fully understand this, but I think I got you homie:

@echo off
for /R "[Insert Path to main folder here]\" %%I in (*.chd) do for %%J in ("%%~dpI.") do echo %%~nxI>>"%%~dpI%%~nxJ.m3u"

If you tell me the path I can insert it for you. Otherwise, insert the exact absolute path to the main folder with all your subdirectories, and of course remove the brackets. Remember this only works if the chds are each in their own folder, if you google "file2folder" there is a nifty little free program that puts every file in a given directory into a folder in seconds. You could then run the corrected bat and just remove the files from the folders and delete them if you like having all your games in a single folder.

I don't have CHDs but I was betting it didn't matter what extension the files were as long as it was expressed in the code, so I tested this by making a folder on my desktop and putting a random rom with a .tap extension inside it and changing the code to look for .tap. And yes, at this moment I do feel like a genius haha.   

This is what the code looked like for me:

@echo off
for /R "C:\Users\Username\Desktop\New folder\Main directory\" %%I in (*.tap) do for %%J in ("%%~dpI.") do echo %%~nxI>>"%%~dpI%%~nxJ.m3u"

Inside that main directory folder I placed a single rom folder with a single rom in it and the modified bat created an m3u with that .tap rom inside it. So it does work. And you don't have to rename any folder to "main directory" or anything, just put the path to the directory that holds all the subdirectories of games. Just to be safe, put the actual bat file in that same directory before you run it. And that last slash after the directory is necessary in the code. 

Edited by fromlostdays
  • Like 1
Link to comment
Share on other sites

19 hours ago, fromlostdays said:

The guy who wrote the code said something: 

I don't fully understand this, but I think I got you homie:

@echo off
for /R "[Insert Path to main folder here]\" %%I in (*.chd) do for %%J in ("%%~dpI.") do echo %%~nxI>>"%%~dpI%%~nxJ.m3u"

If you tell me the path I can insert it for you. Otherwise, insert the exact absolute path to the main folder with all your subdirectories, and of course remove the brackets. Remember this only works if the chds are each in their own folder, if you google "file2folder" there is a nifty little free program that puts every file in a given directory into a folder in seconds. You could then run the corrected bat and just remove the files from the folders and delete them if you like having all your games in a single folder.

I don't have CHDs but I was betting it didn't matter what extension the files were as long as it was expressed in the code, so I tested this by making a folder on my desktop and putting a random rom with a .tap extension inside it and changing the code to look for .tap. And yes, at this moment I do feel like a genius haha.   

This is what the code looked like for me:

@echo off
for /R "C:\Users\Username\Desktop\New folder\Main directory\" %%I in (*.tap) do for %%J in ("%%~dpI.") do echo %%~nxI>>"%%~dpI%%~nxJ.m3u"

Inside that main directory folder I placed a single rom folder with a single rom in it and the modified bat created an m3u with that .tap rom inside it. So it does work. And you don't have to rename any folder to "main directory" or anything, just put the path to the directory that holds all the subdirectories of games. Just to be safe, put the actual bat file in that same directory before you run it. And that last slash after the directory is necessary in the code. 

Thanks! That worked for me well sorta if I did everything correct. Basically I took all my multi disc chds and moved them to a temp folder, edited the bat like with the code you provided with the temp directory and ran the bat it worked great except it put the m3u files inside the game folders which wasn't a big deal as I just moved the chds and m3u files back to my PS1 folder.

  • Like 1
Link to comment
Share on other sites

Yeah, my own setup I keep every game (for every system) in their own folder, so when I posed the question I asked for the bat to take into account that directory structure. So you'll have to mimic that structure at least long enough to use the bat, but its easy enough to pull the m3us and games back into one directory if that's your jam. I'm glad you got it working! 

Edited by fromlostdays
  • Like 1
Link to comment
Share on other sites

1 hour ago, fromlostdays said:

Yeah, my own setup I keep every game (for every system) in their own folder, so when I posed the question I asked for the bat to take into account that directory structure. So you'll have to mimic that structure at least long enough to use the bat, but its easy enough to pull the m3us and games back into one directory if that's your jam. I'm glad you got it working! 

Appreciate it :)

  • Game On 1
Link to comment
Share on other sites

  • 8 months later...
  • 2 years later...

Late, but I thought adding this line before the for loop is helpful.

@echo off
del /S *.m3u
for /R "%~dp0" %%I in (*.cue) do for %%J in ("%%~dpI.") do echo %%~nxI>>"%%~dpI%%~nxJ.m3u"

It deletes any old m3u files beforehand. I found that if you ran it twice, it would just add the same lines again and confuse things.

Edited by rednecked_crake
  • Thanks 1
Link to comment
Share on other sites

  • 4 months later...

I use this one for Windows PowerShell (adjust the Path)
 

Get-ChildItem -Path "E:\dreamcast" -Recurse -Filter *.cue | ForEach-Object {$m3uPath = $_.FullName.Replace(".cue", ".m3u"); if (!(Test-Path $m3uPath)) {New-Item -ItemType File -Path $m3uPath -Force | Out-Null}; Add-Content -Path $m3uPath -Value $_.Name}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...