Search Folder and Rename files

Hi,

Im looking for some help with an addition to a larger apple script, ive seen lots of similar queries but none that quite fit the bill, so if anyone can help or direct me to an answer it would be a huge help,

I wanting to follow this general premise

display dialog “Choose Name” default answer “”
set ChosenName to text returned of result

set ImagesFolder to (choose folder with prompt “Choose Images Folder:”)

The bit im struggling with

if the ImagesFolder contains a folder named “Image Set 1” then
look through the folder “Images Set 1” and rename the contents using this logic

if file name conatins 0001_ rename file to ChosenName & “front”
if file name conatins 0002_ rename file to ChosenName & “Back”
if file name conatins 0003_ rename file to ChosenName & “Top”
if file name conatins 0004_ rename file to ChosenNamet & “Bottom”

else

if the ImagesFolder contains a folder named “Image Set 2” then
look through the folder images 2 and rename the content using this logic

if file name conatins 0001_ rename file to ChosenName & “F”
if file name conatins 0002_ rename file to ChosenName & “B”
if file name conatins 0003_ rename file to ChosenName & “T”
if file name conatins 0004_ rename file to ChosenNamet & “B”

(The unqiue characters im using to identify these files are always the last characters if this helps)

Thanks
P

Something like this would do as you requested:

set fs to {}

tell application "System Events"
	tell the folder named "Image Set 2" in ¬
		the folder named ImagesFolder to if exists then ¬
		set [fs, suffix] to [its files where visible = true, ¬
			"F Ba T Bo"]
	
	tell the folder named "Image Set 1" in ¬
		the folder named ImagesFolder to if exists then ¬
		set [fs, suffix] to [its files where visible = true, ¬
			"front,back,top,bottom"]
	
	repeat with f in fs
		set filename to f's name
		set extension to "." & f's name extension
		
		if the filename contains "0001_" then
			set f's name to ChosenName & "_" & ¬
				suffix's first word & the extension
			
		else if the filename contains "0002_" then
			set f's name to ChosenName & "_" & ¬
				suffix's second word & the extension
			
		else if the filename contains "0003_" then
			set f's name to ChosenName & "_" & ¬
				suffix's third word & the extension
			
		else if the filename contains "0004_" then
			set f's name to ChosenName & "_" & ¬
				suffix's fourth word & the extension
		end if
	end repeat
end tell

I took the liberty of placing an underscore between ChosenName and its new suffix, otherwise it would look a bit weird, e.g. Newname_front.jpg vs Newnamefront.jpg. I also adjusted the suffixes for Image Set 2, as you had repetition, i.e. "F", "B", "T", "B" became "F", "Ba", "T", "Bo".

Note, also, that if both folders "Image Set 1" and "Image Set 2" exist, then this will only operate on the files contained in "Image Set 1", as that was what you requested.

Thanks for this, The additional amends where just what i needed as well :grinning:
Thanks again P