Trim filenames in multiple subfolders

Hi,
I never used AppleScript but now I have a problem where I could really need it. I have to trim the filenames of about 100,000 files which are placed in 400 different folders. With the script “Trim filenames” from Apple I am only able access all files from one folder but of course I would like to access all 400 folders. I have spent some time now but I could not figure it out. :oops:
Any suggestions :?:
Christian

Code:

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
repeat
display dialog “Text to trim from every file name:” default answer “” buttons {“Cancel”, “Trim Start”, “Trim End”}
copy the result as list to {the text_to_trim, the button_pressed}
–if the button_pressed is “Cancel” then return “user cancelled”
if the text_to_trim is not “” then exit repeat
end repeat
set the character_count to the number of characters of the text_to_trim
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
if folder of this_info is false then
set the current_name to the name of this_info
if the button_pressed is “Trim Start” then
if the current_name begins with the text_to_trim then
set the new_name to (characters (the character_count + 1) thru -1 of the current_name) as string
set_item_name(this_item, new_name)
end if
else
if the current_name ends with the text_to_trim then
set the new_name to (characters 1 thru -(the character_count + 1) of the current_name) as string
set_item_name(this_item, new_name)
end if
end if
end if
end repeat
beep 2
:oops:

What you’re asking for is not a simple script.

If you’re asking how to handle open subfolders of folders; that technique is called recursion.


on open dropped_list  
-- drag an item onto the icon of this 
-- script when the script is saved as an applet
	repeat with one_item in dropped_list
		if (info for (contents of one_item))'s folder then recurse_folder of me at (contents of one_item)
		-- if the item is a folder, examine its contents and subfolders
		make_unix_compatible of me at (contents of one_item)
		-- then change its name
	end repeat
end open

to recurse_folder at this_item
	tell application "Finder" to set file_list to every file of this_item
	-- get every file in this folder
	repeat with one_file in file_list
		make_unix_compatible of me at this_item
		-- and fix it
	end repeat
	tell application "Finder" to set folder_list to every folder of this_item
	-- get every subolder in this folder
	repeat with one_folder in folder_list
		recurse_folder of me at (contents of one_folder)
		-- and examine them
	end repeat
end recurse_folder

I HAVE NOT included my make_unix_compatible handler, as you’ll want to write your own handler with its own name for your own modifications.

If you’re asking about trimming all the filenames… be aware you’ll need to write an error handler that will (1) handle what happens when the trim produces duplicate file names and (2) log exceptions for you to check by hand.