I’m working on a script that will rename some files by adding an extension to the existing filename, for example, it might rename a file named “SampleDocument.doc” to “SampleDocument.doc.wpd” by adding the “.wpd” extension to the existing “.doc” extension. (The reason I want to do this is that old WordPerfect for DOS files were created with any extension the user wanted, so a WordPerfect “.doc” file can be in WordPerfect format, not Word format.)
However, if the file “SampleDocument.doc.wpd” already exists in the same folder, I want to rename the file “SampleDocument.doc 1.wpd” or something similar to that. If “SampleDocument.doc 1.wpd” already exists, I want to rename it “SampleDocument.doc 2.wpd” etc.
I have been using an ingenious routine written by Smokey Ardisson in the script linked here:
http://dl.dropbox.com/u/271144/Batch_wpd2sxw_rel.zip
But I wonder if a simpler routine is possible. This forum seems to be the world’s best source for simple, elegant code.
Thanks for any help.
“Here’s one I did earlier”
---------------------------------------------------------------
-- See if a file with the passed name exists
-- If so, add an integer to the name, and check again
-- Keep checking until an unused name is found
-- Parameters: path of container as text
-- file name, no extension
-- file extension
-- Returns: updated file name
---------------------------------------------------------------
on getEmptyPath(containerPath, fileName, Ext)
set filePath to containerPath & fileName
set Ext to "." & Ext
tell application "Finder"
if exists file (filePath & Ext) then
set x to 1 -- start counting
repeat
set cFilePath to filePath & " " & x & Ext
if exists file cFilePath then -- found empty spot?
set x to x + 1-- no, keep counting
else -- yes, leave
exit repeat
end if
end repeat
return fileName & " " & x -- this name is safe to use
else
return fileName -- use original name
end if
end tell
end getEmptyPath
I shouldn’t speak as to its elegance, but simpler it is…
(That script you linked to is rather messy, imho.)
That is exactly what I hoping to find! Thank you. This is perfect - and certainly elegant enough for me…!
Even if it’s elegant enough for emendelson i still want to post another way of doing this (for other readers).
on createNewFileName(fileLocation, fileName, fileExtension)
set x to 1
if fileExtension = null or fileExtension = "" then
set fileExtension to ""
else
set fileExtension to "." & fileExtension
end if
set existingFiles to list folder fileLocation with invisibles
set availableFileName to fileName & fileExtension
repeat
if availableFileName is in existingFiles then
set availableFileName to fileName & space & x & fileExtension
set x to x + 1
else
return availableFileName
end if
end repeat
end createNewFileName
The main difference is that code only opens the directory content once and saves it. Then it checks in it’s stored data if a file exists. Another minor difference is that it works with extension and extenionless files like directories for instance.
That’s a very nice variation - thank you again!