Changing File Properties in an m4a music file using VBA...

Lidsavr

Active Member
Joined
Jan 10, 2008
Messages
330
I recently lost over 10,000 music files and my backup was corrupted, so I had to recover the files using a third party program. This was successful. Unfortunately, I lost all of the filenames. Each music file still contains it's original file properties meaning the Title of the song still exists. I want to copy the Title of the song (from the file properties and use that as the filename. I have played around with coding using the following, but I get an error. I have all of the files stored in the root of my D:\ drive.

Will someone look at the code and make suggestions? Thank you!

VBA Code:
Sub RenameFile()
    Dim fso As Object
    Dim folder As Object
    Dim file As Object
    Dim ext As String
    Dim newName As String
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder("D:\")
    
    For Each file In folder.Files
        ext = fso.GetExtensionName(file.Path)
        If ext = "m4a" Then
            newName = file.ParentFolder.Path & "\" & file.Title & ".m4a"
            Name file.Path As newName
        End If
    Next file
    
    Set fso = Nothing
    Set folder = Nothing
    Set file = Nothing
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
The File object that you're using doesn't have a title property - it doesn't exist. It's not simply a matter of using the name of the data item you want - you have to make sure the code your using can access it. I have recently written some code to extract metadata details from XLSM workbooks that can be adapted slightly to work for M4A files too:

VBA Code:
Function GetExtendedProperty(ByVal FileName As String, ByVal TargetProperty As String)
    If LenB(Dir(FileName)) Then
        Const ssfDESKTOP As Variant = 0
        Dim Result As Variant
        Result = CreateObject("Shell.Application").Namespace(ssfDESKTOP).ParseName(FileName).ExtendedProperty(TargetProperty)
        If IsArray(Result) Then Result = Join(Result, ";")
        GetExtendedProperty = Result
    End If
End Function

You can call it, for example, like:

VBA Code:
Sub TestExtract()
    Dim Title As string
    Title = GetExtendedProperty("D:\testfilename.m4a", "System.Title")
    MsgBox Title
End Sub

I've adjusted your code slightly so that it calls the above function for you - just bear in mind that it will use the title of the m4a as it is stored in the file... that means what is stored as a title may not necessarily work as valid file name - but try it and see and let us know the results. I should stress that what you're doing can't be undone, so you should always test code on sample files first, and you should always keep backups.

VBA Code:
Sub RenameFile()
    Dim fso As Object
    Dim folder As Object
    Dim file As Object
    Dim ext As String
    Dim newName As String
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder("D:\")
    
    For Each file In folder.files
        ext = fso.GetExtensionName(file.Path)
        If ext = "jpg" Then
            newName = file.ParentFolder.Path & "\" & GetExtendedProperty(file.Path, "System.Title") & ".m4a"
            Name file.Path As newName
        End If
    Next file
    
    Set fso = Nothing
    Set folder = Nothing
    Set file = Nothing
End Sub

I've tested it on two dozen m4a files on my computer and it worked for me - hopefully it does what you need.
 
Upvote 0
Solution
Thank you for your reply.

I've tried the code over and over, making small adjustments and then changing it back. I get a repeated error, which says, "Compile Error. Sub or Function Not Defined."
The function that is not defined (highlighted by the error) is: GetExtendedProperty (on the line after the If ext="jpg" Then line).

The only changes I made to the code are the path (Set folder = fso.GetFolder("D:\). I moved the files so they were not sitting in the root drive, so now they are in E:\m4a\
I also changed the If ext = "jpg" Then to If ext = m4a Then (all of the files in the folder are m4a files.

Do you know why the error occurs at GetExtendedProperty ??

Thank you for your assistance.
 
Upvote 0
It would only show that error message if you didn't copy across the code I provided at the top of my reply.
 
Upvote 0
Dan_W,
I didn't understand how a Function command works within VBA code. I went back reran the whole code and it works perfectly!

Thank you
 
Upvote 0
I have run into a problem. Not with the code; it works perfectly as written. The problem I am having is that often Music publishers use punctuation in the Title of a song that is not allowed in a Windows filename.When it attempts to create the filename from the Title, I get an error.

I am seeing a lot of forward slashes (/), colons :)) and question marks (?). Once the Title has been put into a variable, is there a way to evaluate it and have these characters changed to a hyphen (-)?
 
Upvote 0
just bear in mind that it will use the title of the m4a as it is stored in the file... that means what is stored as a title may not necessarily work as valid file name
I did have a sneaking suspicion this would happen. ;)

Yes, it's easy enough to deal with. Will dig something out when I get home from work in half an hour.
 
Upvote 0
Haven't made it home, yet, but I found some code that should help. Post in thread 'Remove all restricted characters from save file name' Remove all restricted characters from save file name

You would call the function over the retrieved title of the file, and not the entire file path, because it will remove the required \ for example from the path, if that makes sense.
 
Upvote 0

Forum statistics

Threads
1,215,101
Messages
6,123,095
Members
449,095
Latest member
gwguy

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