Jump to content

Search and replace file name


dash86
 Share

3 posts in this topic

Recommended Posts

Hi,

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:

123456-Todo-KD-CPY
123457-done-KD-CPY
123458-CPY
123459-NEW-CPY

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 
   else 
       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

 
Link to comment
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.

MusicEnding.app.zip

Link to comment
Share on other sites

 Share

×
×
  • Create New...