Rename file based on folder name

I have a folder containing folders, each of which has a few files in it, one file having a specific extension, say “ext”.





What I want is to name the .ext file after its containing folder, move that newly-renamed .ext file out of its containing folder into the folder containing that folder, and delete the folder left behind. I’d be left with:



Here’s what I’ve tried (using “testfolder” as the folder containing my folders of files):

tell application “Finder”
set theFolders to get (every folder of ((choose folder) as string))
repeat with aFolder in theFolders
set Base_Name to my aFolder
set thefiles to {get (every file in folder aFolder whose name extension is “flv”)}
repeat with aFile in thefiles
set aFile’s name to (Base_Name & “.” & (aFile’s name extension))
end repeat
end repeat
end tell

The result is an error message saying "Can’t get every folder of “Macintosh_HD:testfolder:”

Ideas, anyone?

You could use this:

set mainFolder to (choose folder)
tell application "Finder"
	set subFolders to every folder of entire contents of mainFolder as string
end tell



try this, it uses a shell command to filter the files, change the literal string in the property line to your desired extension

property ext : "txt"

set baseFolder to POSIX path of (choose folder)
set theFiles to paragraphs of (do shell script "/usr/bin/find " & quoted form of baseFolder & " -type f -name '*." & ext & "'")
repeat with oneFile in theFiles
	set aFile to POSIX file oneFile as alias
	tell application "Finder"
		set c to name of container of aFile
		set name of aFile to c & "." & ext
	end tell
end repeat

Hi Stefan,
Nice way of doing this.

Stefan’s already given you a good alternative, but on an educational note, the error message is because you’ve coerced the ‘(choose folder)’ result to string, so the line’s trying to get the folders of a string instead of the folders of a folder. It should just be:

set theFolders to every folder of (choose folder)

After that, there are more errors waiting to kick in. The script you were trying to write should have looked something like this (tested in OSX 10.6.2):

set targetExtension to ".ext" -- or whatever you need it to be.
set baseFolder to (choose folder)

tell application "Finder"
	set theFolders to every folder of baseFolder
	repeat with aFolder in theFolders
		set Base_Name to name of aFolder
		-- You say only one file in the folder has the target extension.
		-- (. 'whose name extension is "ext"' doesn't work on my machine, for some reason.)
		set theFile to (first file in aFolder whose name ends with targetExtension)
		-- The Finder returns name references, so here the file's moved before renaming it.
		-- 'move' returns a reference to the file in the new location.
		move theFile to baseFolder
		set result's name to (Base_Name & targetExtension)
		(* Alternatively, instead of the last two lines:
		set newName to Base_Name & targetExtension
		set theFile's name to newName
		move file newName of aFolder to baseFolder *)
	end repeat
	delete theFolders
end tell