Rename part of a file name

Hi,

Hopefully someone can help me here as i’m somewhat stuck,

We have pdf files that have a naming convention of “PXXX_YYYY_JAN09” and I need to change them to “PXXX_YYYY_JAN10”

Does anybody have a script that will help preferably a folder action script?

Thanks in advance…

just get the name of the file, set the part u want to get rid of as the text item delimiter, and change the filename to the part of the name u want to keep, and then whatever you want to change the rest of it to, and then the file extension:

tell application “Finder”

set fileAlias to (choose file)
set filePath to fileAlias as text
set TID to AppleScript's text item delimiters

set AppleScript's text item delimiters to ":"
set fileName to last text item of filePath

set AppleScript's text item delimiters to "."
set theExtension to last text item of fileName
set fileNamePart1 to text item 1 of fileName

set AppleScript's text item delimiters to "CHANGEME"
set fileNamePart1 to text item 1 of fileNamePart1

set fileNamePart2 to "CHANGED"
set fileName to fileNamePart1 & fileNamePart2 & "." & theExtension

set AppleScript's text item delimiters to TID
set name of fileAlias to fileName

end tell

oh, I wrote that without reading ur message in full. It is much easier than what I just did:


tell application "Finder"
	
	set fileAlias to (choose file)
	set filePath to fileAlias as text
	set TID to AppleScript's text item delimiters
	
	set AppleScript's text item delimiters to ":"
	set fileName to last text item of filePath
	
	set AppleScript's text item delimiters to "."
	set theExtension to last text item of fileName
	set fileNamePart1 to text item 1 of fileName
	
	set AppleScript's text item delimiters to "JAN09"
	set fileNamePart1 to text item 1 of fileNamePart1
	
	set fileNamePart2 to "JAN10"
	set fileName to fileNamePart1 & fileNamePart2 & "." & theExtension
	
	set AppleScript's text item delimiters to TID
	set name of fileAlias to fileName
	
end tell

and if you need assistance turning it into a folder action, just reply.

Or if you have the standard menu item scripts loaded then you can use:
Finder Scripts>Replace Text in Item Names
This should do the job for you

I’m guessing that you don’t just want to change JAN09 to JAN10 but also FEB09 to FEB10 and so on. If you find and replace “09” with “10” that will do it but is there any chance that “09” will appear anywhere else in the file name?

What else can you tell us about the rules you want to follow for this? Is the “09” always the last two characters of the filename before the extension? Can “09” appear in the rest of the filename anywhere?

Sounds like you might want something more like (in pseudocode - sorry I don’t have time right now to work out the actual code):

Separate the filename from the extension
If the last 2 characters of the filename = “09” the set a string to characters 1 through the total characters minus 2
Final string = string you just made + “10” + “.” + extension.