Increment filename if file exist

Hello,
I would like to found an AppleScript that check if a file exist in a specific folder (no problem some of you have already help me to do that) and then increment filename just before the suffix. For example: I have the file "pdf.pdf" and I want to move it on a folder then if a file with the same filename exist, the script rename the file to "pdf1.pdf" but also if a file "pdf1.pdf" is already in the folder, i need that it rename the file to "pdf2.pdf" etc...
Someone know how to do that?
Thanks
Jerome

Posted on Sep 27, 2005 1:58 PM

Reply
8 replies

Sep 27, 2005 3:56 PM in response to Jerome Curty

[EDIT]
I edited the script below after posting it to be a bit cleaner. Probably still could use some cleaning up but it's better than it was 😉

Hi Jerome,

Maybe this script will get you started in the right direction:

set File2Move to choose file without invisibles
set DestFolder to choose folder
set FileInfo to info for File2Move
set FileFullName to name of FileInfo
set FileDest to DestFolder & FileFullName as string
try
FileDest as alias
set FileExt to name extension of FileInfo
set FileName to text 1 through -((length of FileExt) + 2) of FileFullName
set NewFilePath to RenameFile(DestFolder, FileName, FileExt)
on error
set NewFilePath to FileDest
end try
return NewFilePath
on RenameFile(ParentFolder, FileName, FileExt)
set x to 1
repeat
set NewFilePath to ParentFolder & FileName & x & "." & FileExt as string
try
NewFilePath as alias
set x to x + 1
on error
exit repeat
end try
end repeat
return NewFilePath
end RenameFile

Hope this helps...

Sep 28, 2005 2:34 AM in response to Chachi

Hi Chachi,
I edited your post to make this script but it seems that it doesn't work. Could you help me to find what's going wrong?

Thanks,
Jerome

on open theseItems
set File2Move to theseItems without invisibles
set DestFolder to "Spool:Essai:Essai Jerome sortie:"
set FileInfo to info for File2Move
set FileFullName to name of FileInfo
set FileDest to DestFolder & FileFullName as string
try
FileDest as alias
set fileExt to name extension of FileInfo
set FileName to text 1 through -((length of fileExt) + 2) of FileFullName
set NewFilePath to RenameFile(DestFolder, FileName, fileExt)
tell application "Finder" to move File2Move to DestFolder
on error
set NewFilePath to FileDest
end try
return NewFilePath
end open
on RenameFile(ParentFolder, FileName, fileExt)
set x to 1
repeat
set NewFilePath to ParentFolder & FileName & x & "." & fileExt as string
try
NewFilePath as alias
set x to x + 1
on error
exit repeat
end try
end repeat
return NewFilePath
end RenameFile

Sep 28, 2005 6:48 AM in response to Jerome Curty

Hi,
I also try this. It function if a file is already in the folder, it will rename it adding "1" before the suffix, but if there is already a file with the "1" before the suffix i've got an error that the file already exist.

tell application "Finder"
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set the target_file to (theseItems) as alias
set FileName to name of target_file
set fileExt to last text item of FileName


set fileExt to last text item of FileName
set nameWithoutExtension to first text item of FileName
set appendStr to 1
repeat with eachItem in theseItems
if (exists file FileName of folder "Spool:Essai:Essai Jerome sortie:") then
set newName to (nameWithoutExtension & appendStr & "." & fileExt)
set name of target_file to newName
move theseItems to folder "Spool:Essai:Essai Jerome sortie:"
else
move theseItems to folder "Spool:Essai:Essai Jerome sortie:"
end if
end repeat
end tell
end open

Sep 28, 2005 9:08 AM in response to Jerome Curty

Hi Jerome,

Let me see if I can help now that I know a bit more about how the script is needing to be implemented. Here's a revision of your edited version of my script that works on my computer. I added a repeat loop just in case you want to drop multiple files onto it. I'm not sure how to script the Finder to move files and rename them (I'm much more comfortable using a quick "do shell script" command). This script should do the trick now:

on open theseItems
set DestFolder to choose folder
repeat with anItem in theseItems
set File2Move to anItem as alias
set FileInfo to info for File2Move
set FileFullName to name of FileInfo
set FileDest to DestFolder & FileFullName as string
try
FileDest as alias
set fileExt to name extension of FileInfo
set FileName to text 1 through -((length of fileExt) + 2) of FileFullName
set NewFilePath to RenameFile(DestFolder, FileName, fileExt)
on error
set NewFilePath to FileDest
end try
do shell script "mv " & quoted form of POSIX path of File2Move & space & ¬
quoted form of POSIX path of NewFilePath
end repeat
end open
on RenameFile(ParentFolder, FileName, fileExt)
set x to 1
repeat
set NewFilePath to ParentFolder & FileName & x & "." & fileExt as string
try
NewFilePath as alias
set x to x + 1
on error
exit repeat
end try
end repeat
return NewFilePath
end RenameFile

Just change the "set DestFolder to choose folder" part of the script to set the variable DestFolder to the actual path to your destination folder.

Let me know if you get stuck again...

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Increment filename if file exist

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.