Move photo number to front of file name

I have aquired over 15,000 photos that have a photo number on the end of the file name, instead of the beginning of the file name.
I want to rename them with that same number on the beginning of the file.
I need to retain the same number.

Example: Camp Gordon, GA 1951_1555.jpg - - - - Original

              1555 Camp Gordon, GA 1951.jpg  - - - - -This is what I am trying to get.

Any help will be greatly appreciated. I have tried writing a script and failed.

Thanks,
LeslieJ

Hi,

assuming the naming scheme is always the same [fileName_number.extension] , try this


set fileName to "Camp Gordon, GA 1951_1555.jpg"
if fileName contains "_" then
	set {TID, text item delimiters} to {text item delimiters, "_"}
	tell text items of fileName to set {part1, part2} to {items 1 thru -2, last item}
	set text item delimiters to "."
	set {photonumber, nameExtension} to text items of part2
	set text item delimiters to TID
	set newName to photonumber & space & part1 & "." & nameExtension
end if

Hi,

Thanks for the help.
The part that you wrote works perfect. However, the rest of it is still a problem.

I can’t get it to write the changed version back to the folder.
And the “repeat” to the next file seems to have trouble too.

Here is what I have so far, including your part:

try
tell application “Finder” to set the source_folder to (folder of the front window) as alias
on error – no open folder windows
set the source_folder to path to desktop folder as alias
end try

set the item_list to list folder source_folder without invisibles
set source_folder to source_folder as string
repeat with i from 1 to number of items in the item_list
end repeat

set this_item to item i of the item_list
set this_item to (source_folder & this_item) as alias
set this_info to info for this_item
set the current_name to the name of this_info

repeat with this_file in {every document of the front window}

set fileName to name of this_info

if fileName contains "_" then
	set {TID, text item delimiters} to {text item delimiters, "_"}
	tell text items of fileName to set {part1, part2} to {items 1 thru -2, last item}
	set text item delimiters to "."
	set {photonumber, nameExtension} to text items of part2
	set text item delimiters to TID
	set newName to photonumber & space & part1 & "." & nameExtension
	
end if

end repeat

G’day Leslie

Try something like this…

Regards

Santa


tell application "Finder"
	set keeplist to (every document of the front window) as alias list
	repeat with this_file in keeplist
		
		set fileName to name of this_file
		
		if fileName contains "_" then
			set {TID, text item delimiters} to {text item delimiters, "_"}
			tell text items of fileName to set {part1, part2} to {items 1 thru -2, last item}
			set text item delimiters to "."
			set {photonumber, nameExtension} to text items of part2
			set text item delimiters to TID
			set newName to photonumber & space & part1 & "." & nameExtension
			set name of this_file to newName
		end if
	end repeat
end tell

A very big THANK YOU to StefanK and Santa for all the help.
The problem has been solved.

LeslieJ