Sunday, May 9, 2021

#1 2011-05-14 09:31:55 am

emendelson
Member
Registered: 2008-10-31
Posts: 376

Rename file but add number if new name already exists?

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.

Offline

 

#2 2011-05-15 06:34:26 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 581

Re: Rename file but add number if new name already exists?

"Here's one I did earlier"

Applescript:

---------------------------------------------------------------
-- 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.)

Offline

 

#3 2011-05-15 08:42:57 am

emendelson
Member
Registered: 2008-10-31
Posts: 376

Re: Rename file but add number if new name already exists?

That is exactly what I hoping to find! Thank you. This is perfect - and certainly elegant enough for me...!

Offline

 

#4 2011-05-16 03:29:34 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2809
Website

Re: Rename file but add number if new name already exists?

Even if it's elegant enough for emendelson i still want to post another way of doing this (for other readers).

Applescript:

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.

Offline

 

#5 2011-05-16 10:39:21 am

emendelson
Member
Registered: 2008-10-31
Posts: 376

Re: Rename file but add number if new name already exists?

That's a very nice variation - thank you again!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)