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

**URL:** <https://resource.dopus.com/t/music-file-cleanup-remove---move-to-artist-folder-add-after/10553>\
**Category:** Rename Presets\
**Tags:** script, regex, vbscript\
**Created:** [April 16, 2011, 8:21pm UTC](https://resource.dopus.com/t/music-file-cleanup-remove---move-to-artist-folder-add-after/10553 "2011-04-16T20:21:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![raydiognome](https://resource.dopus.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@raydiognome](https://resource.dopus.com/u/raydiognome)\
**Post date:** [April 16, 2011, 8:21pm UTC](https://resource.dopus.com/t/music-file-cleanup-remove---move-to-artist-folder-add-after/10553/1 "2011-04-16T20:21:01Z")

</div>

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`

 ![200dpi_wc10308_dopus](https://resource.dopus.com/uploads/default/original/3X/0/8/08b37d8204f8c69784545893acc356f2373643b8.png)

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:

- [Remove\_Create Folders Fix Number.orp](https://resource.dopus.com/uploads/default/original/3X/a/8/a86214b26e2d032b891453366ff82bbc185e0650.orp) (3.3 KB)
- See: [How to use Rename Presets from this forum](https://resource.dopus.com/t/how-to-use-rename-presets-from-this-forum/26726/3)

### 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.

```vbscript
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

```
