Jump to content

Search and replace file name

3 posts in this topic

Recommended Posts


I need a hint. Problem to solve is user inputs a number. Number is being searched in a folder (2015) with one already existing folder named with the same number user inputs. 

lets say user searches for 123456 folder being searched is 2015 and folder to be found is 123456-CPY, Now the script needs to replace CPY with OK.

Files have different legth say the name can be 123456-CPY or the name can be 123456-ToDo-KD-CPY 

In the folder of 2015 I have backups named 123456 or 123457 or 123458 all have different file names like so:


If I am done I just want the CPY changed to OK. I am only searching for the 6 numbers in front so i only type in 123456 but the folder to be renamed could have the extension "done-KD-CPY" or "NEW-CPY" but not both together the number up front is unique. For example I am looking for 221334 in the script (one hit): folder 221334-done-KD-CPY now the name needs to be 221334-done-KD-OK. 

Hope this explains it better and helps.

I would appreciate if you could help out.


set theReturnedItems to (display dialog "Type Number plz" & return & return & "Number: " default answer "123456") 

set numberip to the text returned of theReturnedItems 
set pfad to (choose folder) 
tell application "Finder" 
   set numberip2 to name of folder (pfad & numberip) 
end tell 
rename(numberip2, "ö|CPY| B", "oe|OK|B", "|") 

on rename(input, srchList, replList, separator) 
   if class of srchList is string then 
       set AppleScript's text item delimiters to separator
       set srchList to every text item of srchList 
       set replList to every text item of replList 
       set AppleScript's text item delimiters to "" 
   end if 
   repeat with i from 1 to (count of srchList) 
       set srch to item i of srchList 
       set repl to item i of replList 
       set AppleScript's text item delimiters to srch 
       set the temp to every text item of input 
       set AppleScript's text item delimiters to repl 
       set input to temp as string 
   end repeat 
   set AppleScript's text item delimiters to "" 
   return input 
end rename


Share this post

Link to post
Share on other sites

Try Automator.app and the Rename Finder Items: Replace Text option it has for this purpose. I routinely use it for a similar idea you can put in both of your names to be replaced -done-KD-CPY and NEW-CPY to be replaced with -done-KD-OK in the two Rename items in the .app you get to create from using it. Now a simple drag and drop of the items to be renamed on the created.app will do what you need done. I have attached my simple two item music renaming .app I use to remove the ending I put on directories when encoding my CDs with XLD to get you started. Just change the endings in there by opening it with  Automator.app adding the texts to be replaced and the replacement text instead of the nothing I have it in.


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.