increment and move

I want to save a file in a folder but first I have to check to see if it exists and if if the file already exists it needs to goto another folder no biggie there except if the file exists in the folder that I want it into then I have to increament the file name and try again until it will go into the new folder

if anyone has something that does this that would really helpful


Hi mcgrailm,

What I always say is to use a good naming convention. For instance, if your file names look like this:



Here, you can easily get the index of the last file by just getting the last file because it’s alphabetized. Then you can get the next index with:

set file_name to “file_011.ext”
set next_index to ((word 3 of file_name) as integer) + 1

You then put the new name together depending on your naming convention that is easier to work with.


Hi mm,

Here’s an example. The folowing will create a folder on the desktop with files named with the naming convention.

set dp to (path to desktop as string)
tell application "Finder"
	set main_folder to (make new folder at desktop with properties {name:"Main"})
	repeat with i from 1 to 10
		set file_index to text -3 thru -1 of ("00" & i)
		set file_name to "file_" & file_index & ".txt"
		ignoring application responses
			make new file at main_folder with properties {name:file_name}
		end ignoring
	end repeat
end tell

Since the files are alphanumericized(?), you can get the last index by getting the last file of that folder.

set f to choose folder
tell application "Finder"
	set file_name to (name of last file of f) as string -- name returns unicode text
end tell
set next_index to ((word 3 of file_name) as integer) + 1
set file_index to text -3 thru -1 of ("00" & next_index)
set new_name to "file_" & file_index & ".txt"
tell application "Finder" to make new file at f with properties {name:new_name}

Here, I just created new files, but you can rename files with the same method and move the files to the folder.



again thank you for your assistance.

this is an example of what I ended up going with. Its not exact but its the basis of it

set dt to ("" & (path to current user folder) & "Desktop:")
set temp to dt & "temp:"

tell application "Finder"
	set i to 1
	set test to "test"
	set FinalName to test & i
	repeat while exists (file FinalName of folder temp)
		set i to i + 1
		set FinalName to test & i
	end repeat
end tell
return FinalName

there may be a better way to write that but thats what I came up with