Remove everything from # until file extension?

I want to remove everything after the last occurrence of a # until right before the file extension. It should work on files both with and without file extensions and not do anything at all if no # is found.

Ex 1.
0000 Title#10.wav should return 0000 Title.wav

Ex 2.
0010 Title 2#113 should return 0010 Title 2

Any clues?

Hi there,

I’m a bit of a coding n00b so forgive me if this isn’t the most efficient way, but I think I have a solution for you. I would recommend copying some test files to a folder somewhere before you try it for real.

instructions:

  • open finder window
  • multi select files you want to change (drag a box around them, whatever)
  • run script

I have commented each block so you can see what I’m doing.

Hope it helps!


set fileNames to {}
set extension to {}
set trimmed to {}
set composite to {}
-- set empty lists

tell application "Finder"
	set theItems to selection
	repeat with itemRef in theItems
		set end of fileNames to name of itemRef
	end repeat
end tell
-- highlight desired files

repeat with i from 1 to (count theItems)
	set x to characters -4 thru -1 of item i of fileNames as text
	if x contains "." then set end of extension to x
	if x does not contain "." then set end of extension to ""
	set x to ""
end repeat
-- check if file has extension

repeat with i from 1 to (count theItems)
	if item i of fileNames contains "#" then
		set x to item i of fileNames as text
		repeat
			if x contains "#" then set x to characters 1 thru -2 of x as text
			if x does not contain "#" then exit repeat
		end repeat
		set end of trimmed to x
	end if
	if item i of fileNames does not contain "#" then set end of trimmed to item i of fileNames
end repeat
-- if file has a hashtag, trim it off


repeat with i from 1 to (count theItems)
	if item i of trimmed does not contain "." then set end of composite to item i of trimmed & item i of extension
	if item i of trimmed contains "." then set end of composite to item i of trimmed
end repeat
-- if original file name had extension, add it back on

tell application "Finder"
	repeat with i from 1 to (count theItems)
		set x to item i of theItems as alias
		try
			set name of file x to item i of composite
		end try
	end repeat
end tell
-- rename files


I tested Adam239’s script, which works well. This is a slightly different version FWIW.

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "#"

tell application "Finder"
	set finderSelection to selection as alias list
	
	repeat with aFile in finderSelection
		repeat 1 times
			
			set aName to name of aFile
			set anExtension to (name extension of aFile)
			
			if aName does not contain "#" then
				exit repeat
			else if anExtension > "" then
				set aModifiedName to text item 1 of aName & "." & anExtension
			else
				set aModifiedName to text item 1 of aName
			end if
			
			try
				set the name of aFile to aModifiedName
			on error errorMessage
				display dialog "The following error message was reported:" & linefeed & linefeed & errorMessage buttons {"Cancel", "Continue"} default button 1 with icon stop
			end try
			
		end repeat
	end repeat
end tell

set AppleScript's text item delimiters to ASTID

Thanks guys!!