script to find and trucate files over 27 characters

I am back working in a PC sserver environment
you would think that this issue would be gone by now but it isn’t

here is the issue i am trying to copy items (folders of folders and nested files)

copying goes all good till it finds a file/folder whose name is longer than 31 characters then it craps out and you hafta go find the offending file

i came up with this script but i am missing something as it doesnt actually change it

any help would be greatly appreciated

TIA

(*
Replace Text In Item Names

Copyright © 2001 Apple Computer, Inc.

You may incorporate this Apple sample code into your program(s) without
restriction.  This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours.  You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes.  If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.
*)

--set the source_folder to choose folder with prompt "Folder containing items to edit:"

-- get the path to the folder of the front window
-- if no windows are open, the desktop folder will be used
try
	tell application "Finder" to set source_folder to choose folder with prompt "choose the folder to search"
	
on error -- no open folder windows
	set the source_folder to path to desktop folder as alias
end try
tell me to activate
display dialog "Search and replace in:" buttons {"File Names", "Folder Names", "Both"} default button 3
set the search_parameter to the button returned of the result

(*repeat
	display dialog "Enter text to find in the item names:" default answer "" buttons {"Cancel", "OK"} default button 2
	set the search_string to the text returned of the result
	if the search_string is not "" then exit repeat
end repeat*)

(*repeat
	display dialog "Enter replacement text:" default answer "" buttons {"Cancel", "OK"} default button 2
	set the replacement_string to the text returned of the result
	if the replacement_string contains ":" then
		beep
		display dialog "A file or folder name cannot contain a colon (:)." buttons {"Cancel", "OK"} default button 2
	else if the replacement_string contains "/" then
		beep
		display dialog "A file or folder name cannot contain a forward slash (/)." buttons {"Cancel", "OK"} default button 2
	else
		exit repeat
	end if
end repeat

display dialog "Replace "" & the search_string & "" with "" & the replacement_string & "" in every item name?" buttons {"Cancel", "OK"} default button 2*)

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
	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 displayed name of this_info
	set count_char to count characters of current_name
	set the change_flag to false
	
	if count_char is greater than "27" then
		if the search_parameter is "Folder Names" and ¬
			folder of this_info is true then
			set the change_flag to true
			
		else if the search_parameter is "File Names" and ¬
			folder of this_info is false then
			set the change_flag to true
		else if the search_parameter is "Both" then
			set the change_flag to true
		end if
		if the change_flag is true then
			tell application "Finder" to set label index of this_item to 4 --this number selects the yellow label
			set the new_item_name to (characters 1 thru (count_char) of the current_name) as string
			set new_comment to characters 28 thru count_char of the current_name
			my set_comment(this_item, new_comment)
			my set_item_name(this_item, new_item_name)
		end if
	end if
end repeat

beep 2

on set_comment(this_item, new_comment)
	tell application "Finder"
		set theComment to the comment of this_item
		set the comment of this_item to (theComment & return & new_comment)
	end tell
end set_comment


on set_item_name(this_item, new_item_name)
	tell application "Finder"
		--activate
		set the parent_container_path to (the container of this_item) as text
		if not (exists item (the parent_container_path & new_item_name)) then
			try
				set the name of this_item to new_item_name
			on error the error_message number the error_number
				if the error_number is -59 then
					set the error_message to "This name contains improper characters, such as a colon (:)."
				else --the suggested name is too long
					set the error_message to error_message -- "The name is more than 31 characters long."
				end if
				--beep
				tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
				copy the result as list to {new_item_name, button_pressed}
				if the button_pressed is "Skip" then return 0
				my set_item_name(this_item, new_item_name)
			end try
		else --the name already exists
			--beep
			tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
			copy the result as list to {new_item_name, button_pressed}
			if the button_pressed is "Skip" then return 0
			my set_item_name(this_item, new_item_name)
		end if
	end tell
end set_item_name

Have you tried running this in script editor and watching the event log to see where your script is running into problems?