Change folder names; increasing final number by "1"

I have a large group of folders that are named according to projects, the format is always the same, Four letters, space, three numbers, space, and three more numbers. The final numbers represent the version of the project. I have to increase all the folder names up to the next version, so the final one or two numbers need to increase by one.

For example, PROJ 001 058 must now become PROJ 001 059, or PROJ 010 059 must now become PROJ 010 060.

Is there a way to create an AppleScript to do this, to run through a group of 100-300 folders and increase that final number by 1? Unfortunately, not all the folders have the same last three digits, they range from 055 to 080.

I tried to record a script, but it was an ugly mess. And my search of the discussions didn’t result in the answer.

If someone could help, I’d appreciate it.

let theFolder to choose folder
tell application "Finder" to select next folder
set idx to 1
repeat with this_item in these_files
   set fExt to name extension of (info for this_item as alias)
   tell application "Finder" to set name of contents of this_item to rename Text & "    " & Text & "   " &-and Text & 055 - 080& of ("000" & (idx as string))
   set idx to idx + 1
end repeat
end tell

see? Gibberish.

I didn’t try this. I just wrote it off the top of my head. So give it a try and see how it works. You select the main folder holding all of the subfolders you want to rename. Note: you should probably try it on a copy of the main folder first rather than the real thing… once it works properly then do it on the real thing.

set theFolder to choose folder

tell application "Finder"
	-- get all of the folders within the chosen theFolder
	set subFolders to folders of theFolder
	repeat with aFolder in subFolders
		-- get the name
		set folderName to name of aFolder
		-- calculate the next number making it 3 digits
		set folderNumber to text 10 thru 12 of folderName
		set nextNumber to (folderNumber as number) + 1
		set threeDigitNumber to text -3 thru -1 of ("000" & (nextNumber as text))
		-- create to new name using the 3-digit number
		set text 10 thru 12 of folderName to threeDigitNumber
		-- rename the folder
		set name of aFolder to folderName
	end repeat
end tell

It’s better to start with the highest existing number and work down, to avoid clashes when renaming:

set theFolder to (choose folder)

tell application "Finder"
	set subfolderNames to name of theFolder's folders
	-- This assumes that the folder names have been returned in lexical order.
	-- If not, they'll need to be sorted.
	repeat with i from (count subfolderNames) to 1 by -1
		set thisName to item i of subfolderNames
		set newName to text 1 thru 9 of thisName & text 2 thru 4 of ((1001 + (word 3 of thisName)) as text)
		set name of folder thisName of theFolder to newName
	end repeat
end tell