ScriptSpot

Forum topic · Scripts Wanted

auto-renaming files when duplicate exists

By freemanpro · 2021-12-06

Description

How can I not find a ready-made solution for automatically renaming files when exporting and there are duplicates ?

Comments (1)

Function

obaida · 2022-01-11


	fn CheckName old_name Save_Path Extension = (
    	local digits = #() , pattern_name = ""
     	if not doesFileExist (Save_Path + old_name + Extension) then new_name = old_name else (
			digits = filterString old_name "_"
			if digits.count > 1 and digits[digits.count] as number != undefined then (
				pattern_name = trimright old_name "0123456789"
				new_name = pattern_name + ((digits[digits.count] as number + 1) as string)
				new_name = CheckName new_name Save_Path Extension
			)
			else new_name = CheckName (old_name + "_1") Save_Path Extension
		)
		new_name
	)