Increment file name

Hi guys,

I’m after a lightweight automator script (applescript) to detect filenames like “file_v01”, “document_v03” all ending in “_vXX” when theyre dropped into ti, and theyre to be incremented (“file_v02” to “file_v03”) and there is suprisingly little content out there on this.

I have attempted simply detecting and removing the last two characters from the filename to no avail - it doesnt need to be anything fancy, just _v02 becomes _v03 and so on.

Any help would be great!!

I didn’t saw files with this kind of name for years.
Are you sure that your filenames are really ending with _vXX ?
At this time there is a name extension at the end of most filenames.
azerty_v01.txt is a name of text file
qsdfg_v05.rtf is the name of a RTF file
wxcvb_v08.jpg is the name of a jpeg file
wxcvb_v09.jpeg is also the name of a jpeg file

You may save this script

on open thefiles
	display dialog thefiles as text
end open

as an application then drag and drop one of your files on it.
It will display the file path as it received it.

For instance here it returned :
“SSD 500:Users:me:Desktop:answer Customers.scpt”

Here is a piece of code doing the job in every case.
CAUTION, if the dropped file is numbered 99, say wxcv_v99.txt, it will be renamed wxcv_v00.txt

on run
	set theFiles to choose file with multiple selections allowed
	tell application "System Events"
		set theFolder to path of container of theFiles's item 1
	end tell
	my germaine(theFolder, theFiles)
end run

on open theFiles
	tell application "System Events"
		set theFolder to path of container of theFiles's item 1
	end tell
	my germaine(theFolder, theFiles)
end open

on germaine(this_folder, these_items)
	tell application "System Events"
		repeat with i from 1 to count these_items
			set aFile to these_items's item i
			set oldName to name of aFile
			set nameExt to name extension of aFile
			if nameExt ≠ "" then
				set bareName to text 1 thru -(2 + (count nameExt)) of oldName
			else
				set bareName to oldName
			end if
			set maybe to text -4 thru -1 of bareName
			if maybe starts with "_v" and text 3 of maybe is in "0123456789" and text 4 of maybe is in "0123456789" then
				set num to text -2 thru -1 of maybe
				set num to text -2 thru -1 of ((num as integer) + 101 as text)
				if nameExt ≠ "" then
					set newName to text 1 thru -3 of bareName & num & "." & nameExt
				else
					set newName to text 1 thru -3 of bareName & num
				end if
				set name of aFile to newName
			else
				-- Here the name doesn't match the rule. Don't know what to do with it.
			end if
		end repeat
	end tell # System Events
end germaine

Yvan KOENIG running El Capitan 10.11.0 in French (VALLAURIS, France) jeudi 1 octobre 2015 13:26:17