You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Truncate file name with Automator

Hello,


I have file with names such as

107633_Kane_1

107592_martin_5

108005_Megan_3R

etc.


I would like to batch process them and rename all files keeping only the 6 digits from the left. The remaining part of the file name shall be removed. The truncation could be done by either keeping 6 characters from the left or by removing everything right starting with the underscore.


I have tried the Rename Finder Items action of Automator, but I can't find the correct way to remove everything starting with the underscore (I have tried placeholder like * $ %).


Anyone has an idea for the easiest way to keep 6 character (ideally not using Terminal command as I am not technical) ?


Thanks !

Posted on Mar 14, 2020 3:03 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 14, 2020 6:24 AM

The following AppleScript will prompt for a folder that contains the files in the specified format. It does not do recursion, so ignores folders in the selected folder. It renames the files with just the first six characters, provided the "_" location is after the sixth character, and if it exists, will append the original extension.


Launch the Script Editor from Dock : Launchpad : Other.


Copy/and paste the following AppleScript code into the Script Editor. Click the hammer icon to compile the code and check for errors (there are none). Then click the Run button. You can save this as an AppleScript application to your Desktop, where you can double-click to launch it.


If you wanted an Automator application where you could drag and drop these file formats on it, then that will be different AppleScript code.


set sFolder to (choose folder default location (path to desktop))

tell application "Finder"
	set fLst to (every item in folder sFolder whose kind is not "Folder") as alias list
	if fLst is {} then return
	
	repeat with anItem in fLst
		if (offset of "_" in anItem's name as text) > 6 is true then
			set prefix to characters 1 thru 6 of (anItem's name as text) as text
			set ext to anItem's name extension
			if not ext = "" then set ext to "." & ext
			set name of anItem to prefix & ext
		end if
	end repeat
end tell
return



Similar questions

9 replies
Question marked as Top-ranking reply

Mar 14, 2020 6:24 AM in response to Nicolas M

The following AppleScript will prompt for a folder that contains the files in the specified format. It does not do recursion, so ignores folders in the selected folder. It renames the files with just the first six characters, provided the "_" location is after the sixth character, and if it exists, will append the original extension.


Launch the Script Editor from Dock : Launchpad : Other.


Copy/and paste the following AppleScript code into the Script Editor. Click the hammer icon to compile the code and check for errors (there are none). Then click the Run button. You can save this as an AppleScript application to your Desktop, where you can double-click to launch it.


If you wanted an Automator application where you could drag and drop these file formats on it, then that will be different AppleScript code.


set sFolder to (choose folder default location (path to desktop))

tell application "Finder"
	set fLst to (every item in folder sFolder whose kind is not "Folder") as alias list
	if fLst is {} then return
	
	repeat with anItem in fLst
		if (offset of "_" in anItem's name as text) > 6 is true then
			set prefix to characters 1 thru 6 of (anItem's name as text) as text
			set ext to anItem's name extension
			if not ext = "" then set ext to "." & ext
			set name of anItem to prefix & ext
		end if
	end repeat
end tell
return



Mar 29, 2020 7:14 AM in response to VikingOSX

Hey VikingOS,


if you are still around, I do have another, yet very similar task to perform on ~2000 file names which could probably be solved with an AppelScript.


Here are some example file names:

0a108ff445800d0ee2842cf10738f090143e6091_melly_2_100902_1__1b3b6e2e1dd6df53627497ce125d73e6

00d68364cb5b8b6dfd568d5184a7fe1503e19c30_dave_5_103124_1__b481eae0451aa666ad0ffb7a77601b03

1abbf75a68460e72792a0718bc77736f39299957_100124_martin_15_3__abfeeeb5f35162b8db8c53c12d8ac1ce


What I need to do is

  • remove the 40 left character plus the underscore (_), so in fact 41 character, and the underscore is always the limit, and
  • I also need to remove the 32 right character plus the double underscore (__), so in fact 34 character, and the double underscore is always the limit.


The expected result for the three names above are


melly_2_100902_1

dave_5_103124_1

100124_martin_15_3


If you have a smart hint for me, like last time, it would be very cool.


Have a nice Sunday

N

Mar 14, 2020 7:23 AM in response to VikingOSX

Same result, less AppleScript overhead by removing redundant Finder name and name extension calls.


-- Rename files of format xxxxxx_xxx which may or may not have an extension to
-- first six characters preceding the "_", and if it exists, the extension.
-- Second version which is more efficient by reducing calls to name and name extension.

set sFolder to (choose folder default location (path to desktop))

tell application "Finder"
	set fLst to (every item in folder sFolder whose kind is not "Folder") as alias list
	if fLst is {} then return
	
	repeat with anItem in fLst
		set name_ext to anItem's name
		set ext to anItem's name extension
		if (offset of "_" in name_ext) > 6 is true then
			set prefix to (characters 1 thru 6 of name_ext) as text
			if not ext = "" then set ext to "." & ext
			set name of anItem to prefix & ext
		end if
	end repeat
end tell
return


Mar 29, 2020 8:02 AM in response to Nicolas M

The following script prompts for a folder containing the files, just as before. It assumes that the first underscore is always at character location 41, but the "__" has a variable character location in the provided examples. So, I trim off the first characters, leaving the desired name with its trailing character string in the variable temp.


I make a new list from the temp string by breaking on the "__" character forming the desired final name as the first element, and all of the text after the "__" location as the second element. The first item of that list is returned for the file renaming.


Tested with your three name strings, and obtained the desired rename string.


set sFolder to (choose folder default location (path to desktop))

tell application "Finder"
	set fileLst to (every item in folder sFolder whose kind is not "Folder") as alias list
	if fileLst is {} then return
	
	repeat with anItem in fileLst
		set fname to anItem's name
		if (offset of "_" in fname) = 41 and fname contains "__" then
			set name of anItem to my trim_name(fname) as text
		end if
	end repeat
end tell
return

on trim_name(aname)
	set first_trim to (offset of "_" in aname) + 1
	-- cannot depend on "__" being in the same character location for each file
	set temp to characters first_trim thru -1 of (aname as text) as text
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "__"}
	-- splits into a list bearing the name string before the "__", and the string after
	set final_name to item 1 of (text items of temp) as text
	set AppleScript's text item delimiters to TID
	return final_name as text
end trim_name


Truncate file name with Automator

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