changing file names to the name of the containing folder?

i’m definitely a newbie when it comes to automator and scripting, so i apologize if this is basic, but i was just wondering if anyone could help me out-

i’m working on reorganizing video files from a website. for some of the media directories, the files have very generic filenames, so the containing subfolder is the name of the file, and not the file itself (a normal organization for this site is eventname>filename>h/m/l). what i’m trying to do is find a way to replace the name of the highest-res file with the name of the containing folder, and then dump the renamed files all into one directory.

is there a script or series of commands in automator that would automatically replace text in a filename with the name of the containing folder?


here’s a little script i wrote.
It’s not exactly what you’re looking for.
Nevertheless the mechanic is similar to what you want to achieve.
You just have to adapt it to your needs by rewriting around the function texttolist() and set thefoldername

In my case, i added a tag to an audio file by using the name of the container (folder).

Here it is. (I added comments to make it easier to understand)

tell application "iTunes"
	-- we select the current playlist
	set thisPlaylist to (get view of front window)
	-- we select the tracks we're working with.
	if exists selection then
		copy selection to selectedTracks
		copy (get every track of thisPlaylist) to selectedTracks
	end if
	-- for each file, if it's an audio file
	repeat with aTrack in selectedTracks
		if class of aTrack is file track then
          we get the name of the container by using the "texttolist" function below
        NB: the place of the file on the hd with osx is written like that
        osx uses ":" as a separator of name in the folder tree
        we start from the file and the file is at the end of the list, so we point to the last item using -1
       'item -1 is the  file, item -2 folder-container of the file, etc.*)
			set theFoldername to item -2 of my TextToList((aTrack's location as string), ":")
			-- we copy the name of the folder to the album tag of the tracks
			copy theFoldername to aTrack's album
		end if
	end repeat
end tell

(* function Text ToList, is used to create a list based on the tree location of your file
the first variable in the brackets is the tracks location as string, the second is our separator (:) *)
on TextToList(thetext, theDelimiter)
	set saveDelim to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {theDelimiter}
		set thelist to every text item of thetext
	on error errStr number errNum
		display dialog "Error"
		set AppleScript's text item delimiters to saveDelim
		error errStr number errNum
	end try
	set AppleScript's text item delimiters to saveDelim
	return (thelist)
end TextToList

I hope it helps.



this is quite easy with plain AppleScript. Can you please post an example with real names

@ igor
Hi Igor,

a little suggestion to your script.
You can replace the whole TextToList subroutine with

set loc to aTrack's location
tell application "Finder" to set theFoldername to name of container of loc