I'm stuck setting file metadata from parent folder name

Hi everyone

I really hope someone can help me, I’ve been going around in circles on this. I’d like to rename all the metadata album names in my music collection to the parent folder name. The iteration that I think is needed is:

for each subfolder
for each file
store subfolder name as string
set file metadata = subfolder string
end
end

So far I’ve managed to iterate through subfolders, but just accessing the files of the folder is where I’m stuck. because I have album year often prefixing the album name, I get an error because it thinks it’s an integer. (after this I’ll still need to change that filename’s metadata, which I’m not at all sure of either)…

on run
set ArtistFolder to (choose folder with prompt “Select the start folder”)
RenameMetadata(ArtistFolder)
end run

on RenameMetadata(aFolder)
tell application “Finder”
set subFolders to every folder of aFolder
repeat with eachFolder in subFolders
my RenameMetadata(eachFolder)
set theItems to every file of folder (eachFolder)
repeat with theFile in theItems
set firstString to name of theFile
log FileName
end repeat
end repeat
end tell
end RenameMetadata

You need to be much more precise in describing what you want to do.
I suspect that you have a folder structure like this:

artist (a folder)

album (a folder)

song (a file)
another song
another artist (a folder)

And so on.
And you want to rename all album folders with the name of the artist?

Metadata = data about data. “filename’s metadata” means nothing; the filename is metadata.
You do not rename the metadata, you rename the file. And doing that replaces the content of the metadata item “filename”.

progress update:

All I need to do is set the metadata ‘album’ tag of a file with a string. Here’s the almost complete solution (within comments is where i need to process metadata)

on run
set ArtistFolder to (choose folder with prompt “Select the start folder”)
RenameMetadata(ArtistFolder)
end run

on RenameMetadata(aFolder)
tell application “Finder”
set subFolders to every folder of aFolder
repeat with eachFolder in subFolders
my RenameMetadata(eachFolder)
set theItems to every file of eachFolder
repeat with theFile in theItems
set strParent to name of eachFolder as string
set strFile to name of theFile as string
(* tell application “iTunes”
set album to strParent
end tell )
(
set album to strParent *)
log strFile
log strParent
end repeat
end repeat
end tell
end RenameMetadata

If I understand your goal, you could avoid iterating folders. I advise testing on a subset of your library, and, more preferably, with dummy files.

set text item delimiters to ":"

tell application "iTunes" to repeat with aTrack in playlist "test"'s file tracks
	set aTrack's album to (aTrack's location as text)'s text item -2
end repeat

thanks for that.I agree on using test files.

I don’t completely understand how to get it working from iTunes, it seems a shame that I can now iterate through every folder and hold the name of the parent folder in a string but still not manage to get that string into the ID3 album name tag. I’ll have to keep coming back to it when I have a spare moment (unless someone can help me…

on run
set ArtistFolder to (choose folder with prompt “Select the start folder”)
RenameMetadata(ArtistFolder)
end run

on RenameMetadata(aFolder)
tell application “Finder”
set subFolders to every folder of aFolder
repeat with eachFolder in subFolders
my RenameMetadata(eachFolder)
set theItems to every file of eachFolder
repeat with theFile in theItems
set strParent to name of eachFolder as string
set strFile to name of theFile as string

			(* ? set ID3 album name tag to strParent *)				
			
			log strFile
			log strParent
		end repeat
	end repeat
end tell

end RenameMetadata

The Finder cannot manipulate ID3 tags. You have to do it in iTunes or to use other third party tools.

ow I see, well that explains my struggles. Thanks so much for that :slight_smile:

One of the 3rd party tools is using mutagen and python on the command line. Here a similar post