batch rename files with folder names?

Hello. I have a couple hundred folders each containing a single mp4 file; the folder names are useful, the mp4 file names not. How can I rename the mp4s with the folder names? That is, I'd like:

Containing folder>Very helpful name>not38helpful_name.mp4

Containing folder>Another helpful name>Reallyuseless3866l_name.mp4

Containing folder>Clear Concise name>How15this%&4helpful.mp4

to be renamed:

Containing folder>Very helpful name>Very helpful name.mp4

Containing folder>Another helpful name>Another helpful name.mp4

Containing folder>Clear Concise name>Clear Concise name.mp4

It would be helpful to then replace the folder with the file (Containing folder>Very helpful name.mp4) but not necessary.

There are no other files within the folders other than the MP4s so no filtering is needed.


thanks

Posted on Oct 15, 2022 07:27 AM

Reply
Question marked as Top-ranking reply

Posted on Oct 15, 2022 02:03 PM

This is relatively easy with a recursive AppleScript, but there are lots of gotchas.


The following script follows your ask - it takes a top-level folder and works its way through the hierarchy, looking for sole .mp4 files, which it then renames according to the folder name.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- prompt for the folder to process
set topFolder to (choose folder with prompt "Select the folder to process:")

my processAFolder(topFolder)

on processAFolder(f)
	tell application "Finder"
		-- get a list of files in this folder
		set folder_contents to every file of f
		-- is there only one file (basic logic check)
		if (count folder_contents) = 1 then
			-- process the file
			my processAFile(item 1 of folder_contents)
		end if
		
		-- now look for any subfolders
		set subfolder_list to every folder of f
		-- iterate through the folders
		repeat with each_folder in subfolder_list
			my processAFolder(each_folder)
		end repeat
	end tell
end processAFolder


on processAFile(theFile)
	tell application "Finder"
		-- is the file a .MP4? 
		if name extension of theFile = "mp4" then
			-- it is, so get the name of its containing folder
			set fn to name of (container of theFile)
			-- and rename the file
			set name of theFile to fn & ".mp4"
		end if
	end tell
end processAFile


There are a few checks - for example if there is more than 1 file in the folder it won't rename (can't rename two files to the same folder name.mp4.


Other than that, it seems to work in my limited testing. Paste the script into a new Script Editor document and click Run.


Similar questions

2 replies
Question marked as Top-ranking reply

Oct 15, 2022 02:03 PM in response to Scott_R

This is relatively easy with a recursive AppleScript, but there are lots of gotchas.


The following script follows your ask - it takes a top-level folder and works its way through the hierarchy, looking for sole .mp4 files, which it then renames according to the folder name.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- prompt for the folder to process
set topFolder to (choose folder with prompt "Select the folder to process:")

my processAFolder(topFolder)

on processAFolder(f)
	tell application "Finder"
		-- get a list of files in this folder
		set folder_contents to every file of f
		-- is there only one file (basic logic check)
		if (count folder_contents) = 1 then
			-- process the file
			my processAFile(item 1 of folder_contents)
		end if
		
		-- now look for any subfolders
		set subfolder_list to every folder of f
		-- iterate through the folders
		repeat with each_folder in subfolder_list
			my processAFolder(each_folder)
		end repeat
	end tell
end processAFolder


on processAFile(theFile)
	tell application "Finder"
		-- is the file a .MP4? 
		if name extension of theFile = "mp4" then
			-- it is, so get the name of its containing folder
			set fn to name of (container of theFile)
			-- and rename the file
			set name of theFile to fn & ".mp4"
		end if
	end tell
end processAFile


There are a few checks - for example if there is more than 1 file in the folder it won't rename (can't rename two files to the same folder name.mp4.


Other than that, it seems to work in my limited testing. Paste the script into a new Script Editor document and click Run.


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

batch rename files with folder names?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.