I cleaned up my mp3 collection - remove track numbers etc. Now I need to rename them

keithmct

Active Member
Joined
Mar 9, 2007
Messages
256
Office Version
  1. 2021
Platform
  1. Windows
Hi all, through some YouTube vids I have succeeded in listing all my mp3's from the music directory H:\Music. The full path shows in column F. Existing track name excluding file type extension is in column A. eg. 01 - Beethoven 9th symphony. New desired file name is in column B eg Beethoven 9th symphony, without file extension.
Subfolders of H:\Music are the usually artist name and sub-sub folders are the albums with the mp3 files. Sometime mp3's don't have an album and are just in the artist folder. Regardless, in column F, I need a way to substitute whatever is between the last backslash and .mp3 with the data in column B.
Thanks in advance
 
Hi
It won't do anything visual except rename the actual files in the locations specified,
i.e. the file in H:\Music\Artist\Album\songtitle.mp3 will be renamed to H:\Music\Artist\Album\songtitle
That's what I understood you wanted, if not, then it's easy to revert back. Let me know what you expected to achieve and we can go from there.
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I think would be better if you post a link to the shared excel file with representative source data and expected result from this example.
Use OneDrive, GoogleDrive, DropBox or any similar
 
Upvote 0
sorry if I wasn't clear. I want to rename the songs without the pesky track numbers in the front of the title. i.e. H:\Music\Artist\Album\ 01 - songtitle needs to be amended to H:\Music\Artist\Album\songtitle.
The mp3 part is not part of the song title, its just the file type which is automatically appended if you choose to have file types visible in windows.
As I mentioned, I have the cleaned up song titles ready to go in a column next to the messy song titles. I also have the full path in a column.
What your first function did was interesting, but if it could cut the songtitle.mp3 from the path and exchange it for the cleaned up one.mp3 and then tell windows to rename the files, that's what I'm trying to achieve.
 
Upvote 0
OK, then it's a simple version of my code!
To clarify, you have the two columns, one with the full path of the existing file, and another with just the cleaned up filename?
i.e.
D3 = H:\Music\Artist\Album\ 01 - songtitle
E3 = songtitle
Is that correct?
If you confirm, I'll update the code accordingly. And you actually want to rename the files to the cleaned one, yes?
 
Upvote 0
that's correct
although the full path also shows the file type extension D3 = H:\Music\Artist\Album\ 01 - songtitle.mp3
 
Last edited:
Upvote 0
OK, based on the detail you have confirmed, this should do what you want assuming you actually want the files on your H: drive renamed?

This is assuming in column D, you have a list of all the original filenames. In column E you have the new filename you want. I didn't check if the new filename has the file extension? Based on your earlier reply, I'm assuming it doesn't and have coded accordingly, but it should work with or without the file extension in column E, i.e. it can either be songtitle or songtitle.mp3

Sub SongRenameV2()

Dim c As Object
Dim i As Integer
Dim intPeriodPos As Integer
Dim strOldSongPath As String
Dim strNewSongPath As String
Dim strNewSongName As String
Dim varSongData As Variant
Dim fso As Object
'Create reference to scripting runtime
Set fso = CreateObject("Scripting.FileSystemObject")
'Define the range to start looking in
Set c = Sheets("Sheet1").Range("D3")
'Begin loop
Do Until IsEmpty(c)
'Get the full file path and the new song name
strOldSongPath = c.Value
strNewSongName = c.Offset(0, 1).Value
'Search for the file extension delimiter
intPeriodPos = InStr(1, strNewSongName, ".", vbTextCompare)
'Only re-add the file extension if missing
If intPeriodPos = 0 Then
strNewSongName = strNewSongName & Right(strOldSongPath, Len(strOldSongPath) - intPeriodPos + 1)
End If
'Split the full path of the song into an array
varSongData = Split(strOldSongPath, "", , vbTextCompare) 'There should be a \ in between the quotes
'Recreate the path
For i = LBound(varSongData) To UBound(varSongData) - 1 Step 1
strNewSongPath = strNewSongPath & varSongData(i)
strNewSongPath = strNewSongPath & "" 'There should be a \ in between the quotes
Next i
'Append the name back
strNewSongPath = strNewSongPath & strNewSongName
'As long as the file exists, rename it
If fso.FileExists(strOldSongPath) Then
fso.MoveFile strOldSongPath, strNewSongPath
End If
'Move object reference
Set c = c.Offset(1, 0)
Loop
'Remove object reference
Set fso = Nothing
End Sub
 
Upvote 0
I tested the code in a new workbook on just 1 album that had all track numbers in front of song title.
run time error 52 bad file name or number
I press debug and yellow highlight at:
fso.MoveFile strOldSongPath, strNewSongPath
 
Upvote 0
I added .mp3 to the end of the clean file names and ran the code. It seemed to be doing something and then threw up the error again. However, it did actually change the name of the song in the music folder. It just doesn't loop to do them all. Tried it again starting at D4 and that worked but also got the error code and only changed the one file name.
 
Last edited:
Upvote 0
Apologies. I've set up the files to match your structure, and fixed a couple of issues in the code.

It works without issue for me for three files listed. If you still get any issues, check none of the files are in use.

Sub SongRenameV2()

Dim c As Object
Dim i As Integer
Dim intPeriodPos As Integer
Dim strOldSongPath As String
Dim strNewSongPath As String
Dim strNewSongName As String
Dim varSongData As Variant
Dim fso As Object
'Create reference to scripting runtime
Set fso = CreateObject("Scripting.FileSystemObject")
'Define the range to start looking in
Set c = Sheets("Sheet1").Range("D3")
'Begin loop
Do Until IsEmpty(c)
'Get the full file path and the new song name
strOldSongPath = c.Value
strNewSongName = c.Offset(0, 1).Value
strNewSongPath = vbNullString
'Search for the file extension delimiter
intPeriodPos = InStr(1, strNewSongName, ".", vbTextCompare)
'Only re-add the file extension if missing
If intPeriodPos = 0 Then
intPeriodPos = InStr(1, strOldSongPath, ".", vbTextCompare)
strNewSongName = strNewSongName & Right(strOldSongPath, Len(strOldSongPath) - intPeriodPos + 1)
End If
'Split the full path of the song into an array
varSongData = Split(strOldSongPath, "", , vbTextCompare) 'There should be a \ in between the quotes
'Recreate the path
For i = LBound(varSongData) To UBound(varSongData) - 1 Step 1
strNewSongPath = strNewSongPath & varSongData(i)
strNewSongPath = strNewSongPath & "" 'There should be a \ in between the quotes
Next i
'Append the name back
strNewSongPath = strNewSongPath & strNewSongName
'As long as the file exists, rename it
If fso.FileExists(strOldSongPath) Then
fso.MoveFile strOldSongPath, strNewSongPath
End If
'Move object reference
Set c = c.Offset(1, 0)
Loop
'Remove object reference
Set fso = Nothing
End Sub
 
Upvote 0
got the same line highlighted in yellow, but now it says file already exists. Files are not in use.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top