Remove part of folder name

Good Evening! I have the following problem:

I have huge amount of Folders. The Folder Names very often include some string: (x21 AB), or (x 213 AB) or (x1130 AB) or any other combination (not more than 10 symbols between "()" totally). The key symbols are ( ) and AB. Any possibility to remove this via Name Mangler? I don't have experience to write the snippet, maybe somebody can help?

Posted on Feb 25, 2019 3:19 PM

Reply
Question marked as Top-ranking reply

Posted on Feb 26, 2019 8:36 PM

OK, given that info, try this:


-- select the top level folder

set base_folder to (choose folder)


tell application "Finder"

-- get a list of the folders in here

set folder_list to every folder of folder base_folder

-- iterate through them

repeat with each_folder in folder_list

-- get the current folder name

set fn to name of each_folder

-- does it contain the string we're looking for?

if (fn contains "(") and (fn contains "AB)") then

-- find the start

set i to offset of "(" in fn

-- and the end

set j to 3 + (offset of "AB)" in fn)

-- construct the new folder name without the (xxx AB) part

set new_name to characters 1 through (i - 1) of fn

if j < (count characters of fn) then

set new_name to new_name & (characters j through -1 of fn)

end if

-- and rename the folder

set name of each_folder to (new_name as text)

end if

end repeat

end tell


The comments should be self-explanatory, but let me know if you need help understanding the flow.

4 replies
Question marked as Top-ranking reply

Feb 26, 2019 8:36 PM in response to esess74

OK, given that info, try this:


-- select the top level folder

set base_folder to (choose folder)


tell application "Finder"

-- get a list of the folders in here

set folder_list to every folder of folder base_folder

-- iterate through them

repeat with each_folder in folder_list

-- get the current folder name

set fn to name of each_folder

-- does it contain the string we're looking for?

if (fn contains "(") and (fn contains "AB)") then

-- find the start

set i to offset of "(" in fn

-- and the end

set j to 3 + (offset of "AB)" in fn)

-- construct the new folder name without the (xxx AB) part

set new_name to characters 1 through (i - 1) of fn

if j < (count characters of fn) then

set new_name to new_name & (characters j through -1 of fn)

end if

-- and rename the folder

set name of each_folder to (new_name as text)

end if

end repeat

end tell


The comments should be self-explanatory, but let me know if you need help understanding the flow.

Feb 25, 2019 3:49 PM in response to esess74

The general concept is pretty straightforward, but there are a number of questions here.


Are all these folders in the same folder? or spread around the disk?

Are they all at the same level, or do you need to dig through multiple levels of folders to find them all?

Where in the name does the relevant string appear? the beginning? the end? in the middle? random?

Are there any cases where either "(", ")", or "AB" appear that you do NOT want to remove?


These are important because they affect how much effort needs to go into the script.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Remove part of folder name

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.