Sorting Away MP3 Files into Sub Directories by Artist Name

Categories: Tools
Tags: No Tags
Comments: 3 Comments
Published on: August 23, 2009

I just published the post where I introduced the great and useful tool Bulk Rename Utility.

I first wanted to include the content of this post into that one as well, but then decided to make it a separate post instead. The introductory post of BRU was already getting long enough, especially because I included suggestions and comments to the tool itself, which I originally intended to post at the tool authors support forums. But I wasn’t able to do that yet, so the post got pretty long and adding something else would just have been too much.

I had a bunch of MP3 files dumped into a folder, several hundred of them. Songs by various artists. They came from some of my old MP3 CD-ROMs that I burned, when I didn’t have that many CDs (compared to today) and keeping things organized wasn’t much of a concern for me.

Well, the amount of CDs grew significantly over the years, so sorting and grouping stuff became necessary to be able to find stuff. Also, the number of different music styles that I learned to enjoy increased. Mixing some of those styles is probably not such a great idea.

The Task at Hand

Anyhow, the file names followed at least some logic. They usually started with the name of the artist followed by the title of the song, separated by a hyphen or dash (-) character. Some also included the album name or release year after the song title, in parentheses or without, spaces between names and the dash separator or without, using underscores “_” instead of spaces or spaces etc.??

There were usually more than one songs per artists, sometimes only 2 or 3 (singles) and sometimes entire albums. I wanted to create folders for each artist and move each MP3 file into the corresponding directory; a problem that I could not solve with the Bulk Rename Utility yet (even though it has the option to extract ID3 meta data from MP3s and then do something with it, which I have not figured out yet).

I did not want to do it by hand, because I still have some old CDs with MP3 files flying around where I would have to repeat this task again. So I ended up writing a little DOS BATCH Script to do the work for me.

MP3 Cleanup/Sorting Batch Script

The code for this script can be found below. You can also download the code via the link below. The ZIP archive contains next to the .BAT script file also a neat little intro to promote my web site hehe.

The batch script performs the following set of tasks:

  1. look for MP3 files in the current directory and process each one of them
  2. look for “-” in the file name (assumed to be the separator between artist name and song title)
  3. parse the file name and extract the string before the first “-“, also trim trailing spaces
  4. checks if directory with that name exists
    1. If not, create the directory (with the artist name)
  5. move the current file into that directory

The source code of MP3FileSort2Dirs.bat

   1:  @echo off
   2:  :: -------------------------------------------------------
   3:  :: MP3 Cleanup/Sorting Batch Script
   4:  :: ------------------------------------------------------- 
   5:  :: 1. looks for MP3 files in current directory
   6:  :: 2. looks for "-" in file name (assumed to be 
   7:  ::    the separator between Artist Name and Song Title)
   8:  :: 3. parses string before first "-", trims trailing spaces
   9:  :: 4. checks if directory with that name exists
  10:  ::   4.1 If not, it creates it (artist name)
  11:  :: 5. moves file into the directory
  12:  :: 
  13:  :: Batch Script by Carsten Cumbrowski aka Roy/SAC
  14:  :: visit
  15:  ??
  16:  setlocal enabledelayedexpansion
  17:  cls
  18:  ::only list files with extension MP3, exclude directories
  19:  for /f "tokens=*" %%A in ('DIR /A-D /ON /B "*.MP3"^|SORT /REVERSE') do (
  20:    call :PERFACTION "%%~A%"
  21:  )
  22:  echo.
  23:  echo Done!
  24:  echo.
  25:  pause
  26:  goto :EOF
  27:  ??
  29:  ::look for dash in file name, parse str before dash
  30:  for /F "tokens=1 delims=-" %%B in ("%~1") do (
  31:  ::trim trailing spaces from parsed string  
  32:      set str=%%~B%
  33:      for /l %%a in (1,1,31) do if "!str:~-1!"==" " set str=!str:~0,-1!
  34:  )
  35:  call :PROCFILE "%str%"
  36:  goto :EOF
  37:  ??
  38:  :PROCFILE
  39:  ::check of directory exists and move file
  40:  set f2="%~1"
  41:  if {%~x1}=={} (
  42:     echo %f2%
  43:     if NOT exist %f2%  MD %f2%
  44:     move "%~1*.MP3" %f2%
  45:  )
  46:  goto :EOF
  47:  ??

I hope you found this post and script useful. Let me know via the comments section of this blog post below.

Thanks and Cheers!

Carsten aka Roy/SAC

  1. Anonymous says:

    Hi Roy!

    Just thought I’d drop a little note letting you know I haven’t used your scripts *yet* but I have been bookmarking some of your posts. I do a bit of data sorting too, and am excited to try these out when the time comes. Keep it up!
    – sP!cRO ex-SAC/ACiD/RMRS/etc 😉

  2. Anonymous says:

    i have a folder with 40000 mp3 your script work great for the last 1000 the rest it won’t work. any idea ? thanks.

  3. Mhh.. Do you have files that use non-basic ASCII characters or special characters like “!”? I don’t think that the number of files is an issue, many files just increase the time it takes for the script to complete.

Leave a Reply to Carsten a.k.a. Roy/SAC Cancel reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

NOTE! I believe in the right for freedom of speech and personal opinion and are against censorship, so feel free to tell me what you think and let me and others hear your opinion on this subject, but please avoid using the f-word and s-word as much as you possibly can, because at the end of the day this blog exists for the purpose of useful exchanges of thoughts, ideas and opinions and not as a valve for your accumulated anger and frustration. Get a shrink for that! Thanks.

Welcome , today is Saturday, July 20, 2024