Search and replace

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

I tried to solve this with shell scripting but I think I am lost right now.


set theReturnedItems to (display dialog "Type Number plz" & return & return & "Number: " default answer "123456") 
		set numberip to the text returned of theReturnedItems

set pfadfolder to POSIX path of "Data_DRIVE:2015-TO-BE-DONE"
	set prodsrch to do shell script "grep " & quoted form of numberip & " " & quoted form of pfadfolder 
		
		set prodsuche to do shell script "echo " & numberip & " | grep -o -e [0-9][0-9][0-9][0-9][0-9][0-9] "
		do shell script " sed -i \"\"  's/" & quoted form of "CPY" & "/" & quoted form of "OK" & "/' " & quoted form of prodsrch

can some one help from what i understand see can search and replace or not?

Hi dashmo.

At its simplest:


set theReturnedItems to (display dialog "Type Number plz" & return & return & "Number: " default answer "123456")
set numberip to the text returned of theReturnedItems

tell application "Finder"
	set name of folder ("Data_DRIVE:2015-TO-BE-DONE:" & numberip & "-CPY") to (numberip & "-OK")
end tell

HI thank you so far haven’t thought it would be that small. unfortunately I get error can not change typ to integer.



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 name of folder (pfad & numberip & "-CPY") to (numberip & "-OK") 
end tell

for the test i changed the path to choose folder action so i can test it better.

I also wonder: Files have different legth say the name can be 123456-CPY or the name can be 123456-ToDo-KD-CPY
So thing working with a text delimiter would be better I came so far





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


But the error with Type integer stays the same with “set numberip2 to name of folder (pfad & numberip)”

Do you have any suggestions ?

Hi.

The error’s in your modification. ‘choose folder’ returns an alias, not text. So concatenating text to this result as in (pfad & numberip & “-CPY”) produces a list containing the alias and the pieces of text, not a path. The easiest thing to do here would be to coerce the ‘choose folder’ result to text when you get it, then the concatenation will produce what’s needed:



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) as text -- Coerce the returned alias to text.
tell application "Finder"
	set name of folder (pfad & numberip & "-CPY") to (numberip & "-OK")
end tell

It would waste people’s time less if things like that were mentioned in the original query.

Hi Nigel,

I much appreciate your help. I really do and I am sorry for the lack of informations in the query. I still hope I can get this to work. The script only works on the files that are named 123456-CPY but those with 123456-ToDo-CPY or with the extension return error can not set name 123456-CPY to 123456-OK. So it is not retrieving the file name right.

I will try it with an hopefully better explanation.

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 there is 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 once more. It would be much appreciated.

Hi dashmo.

Something like this?


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) -- An alias is OK for what follows.

tell application "Finder"
	try
		-- Get the current name of the folder with the required number, if it exists.
		set hitName to name of first folder of pfad whose name begins with (numberip & "-")
	on error
		display dialog "There are no folder names in this folder beginning with "" & numberip & "-"!" buttons {"Cancel"} default button 1 cancel button 1 with icon stop
	end try
	if (hitName ends with "-CPY") then
		-- Make a version of the name with "-OK" at the end instead of "-CPY".
		set newName to text 1 thru -4 of hitName & "OK"
		-- Rename the folder accordingly.
		set name of folder hitName of pfad to newName
	end if
end tell

hi,

yes :slight_smile: