GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Music File Cleanup: Remove '_', move to artist folder, Add '-' after #

Thanks to EVERYONE and especially Leo, I've modified Leo's examples for creating folders, as well as borrowing parts and pieces to create this mutation that will:

  • Remove underscores.
  • Adds a dash between the track number and title.
  • Moves and creates folders in D:\My Music based on artist-album
    (edit the script to change the output folder).

e.g. frank_zappa_-_OZ_-_09_Black_Napkins
becomes D:\My Music\frank zappa\OZ\09 - Black Napkins

If anyone can add fixing the capitalization I'd appreciate it. I tried combining this with Steve's "Title Case" but cannot get them to work together.

Leo's "Sort music files" is similar but I found that a bunch of files I had didn't have the track title in the tag.
Now if we could clean up the filename and THEN tag based on that filename that would be awesome. Almost as awesome as Opus!!

Thanks again for all the examples and sorry if this isn't up to standards or seems repetitive.

Download:

Script code:

Here is the script code for reference. If you just want to use the rename preset, use the download above. The script code is just here to help people browsing the forum for scripting techniques.

option explicit

Function Rename_GetNewName ( strFileName, strFullPath, fIsFolder, strOldName, ByRef strNewName )

	Dim re
	Dim strWordArray
	Dim strExtension
	Dim strNameOnly
	Dim fGotMatch

	fGotMatch = False

	' Create a RegExp object. See http://msdn2.microsoft.com/en-us/library/ms974570.aspx
	Set re = new RegExp
	re.IgnoreCase = True ' Case-insensitive matching.
	re.Global = False ' Don't replace all matches.

	' If we're renaming a file then remove the extension from the end and save it for later.
	if fIsFolder or 0 = InStr(strFileName,".") then
		strExtension = ""
		strNameOnly = strFileName
	else
		strExtension = Right(strFileName, Len(strFileName)-(InStrRev(strFileName,".")-1))
		strNameOnly = Left(strFileName, InStrRev(strFileName,".")-1)
	end if

	' Apply our regular expressions to the filename.

	' look for _, and replace with space
	re.Global = True ' Replace all matches, not just the first " - "
	re.Pattern = "(_)"
	strNameOnly = re.Replace(strNameOnly, " ")

	' "J Trotter - MEBulletin 4 - Qi Chong" -->
	' "J Trotter\MEBulletin\04 Qi Chong"
	re.Pattern = "^([^-]+) - ([^-]+) - ([0-9]) - (.+)$"
	if (re.Test(strNameOnly)) then
		fGotMatch = True
		strNameOnly = re.Replace(strNameOnly, "$1\$2\$3$4")
	else
		' "J Trotter - MEBulletin 04 - Qi Chong" -->
		' "J Trotter\MEBulletin\04 Qi Chong"
		re.Pattern = "^([^-]+) - ([^-]+) - ([0-9]+) - (.+)$"
		if (re.Test(strNameOnly)) then
			fGotMatch = True
			strNameOnly = re.Replace(strNameOnly, "$1\$2\$3$4")
		else
			' "J Trotter - Spring Into" -->
			' "J Trotter\Spring Into"
			re.Pattern = "^([^-]+) - (.+)$"
			if (re.Test(strNameOnly)) then
				fGotMatch = True
				strNameOnly = re.Replace(strNameOnly, "$1\$2")
				end if
		end if
	end if

	if fGotMatch then
		' If the path matched our expected format and was changed,
		' find any remaining " - " in the path and turn them into
		' subfolders.
		re.Global = True ' Replace all matches, not just the first " - "
		re.Pattern = "^(.+) - (.+)$"
		strNameOnly = re.Replace(strNameOnly, "$1\$2")
		re.Global = False
	
		' If the path matched our expected format and was changed,
		' and it starts with a letter or number, move it into
		' a different folder.
		re.Pattern = "^(.)(.+)$"
		strNameOnly = re.Replace(strNameOnly, "D:\My Music\$1$2")
	end if

	' Add -
	re.Global = False ' Don't replace all matches.
	re.Pattern = "([0-9][0-9])(.*)"
	strNameOnly = re.Replace(strNameOnly, "$1 - $2")
	
	' Rejoin the name and extension and we're finished
	strNewName = strNameOnly & strExtension
								
End Function