Skip navigation

Rename files that add a unique ending

309 Views 4 Replies Latest reply: Dec 3, 2012 2:38 AM by MattJayC RSS
MattJayC Level 1 Level 1 (0 points)
Currently Being Moderated
Nov 30, 2012 2:38 AM

This is part of an applescript that I use at the moment,

Currently if a file exists it moves it to the folder rejected instead I want to move it the folder that it exists in then I want to add either _2, _3 etc? and add it to the folder.

 

 

repeat with thisFile in theFilesToMove

                              try

  move thisFile to theDestinationFolder

                              on error

  activate

                                        display dialog "File “" & (name of thisFile) & "” already exists in folder “" & theFolderName & "”. Do you want to add it?" buttons {"Don't replace", "Replace"} default button 2 with icon 1

                                        if button returned of result is "Stop" then

                                                  if (count theLastFolder) is 0 then delete theLastFolder

                                                  return

                                        else if button returned of result is "Replace" then

  move thisFile to theDestinationFolder with replacing

                                        else -- "Don't replace"

  move thisFile to theLastFolder with replacing

                                        end if

                              end try

                    end repeat

iMac, Mac OS X (10.6.8)
  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    Nov 30, 2012 8:07 AM (in response to MattJayC)

    Hi,

     

    Here's a solution:

    -------------------

          repeat with thisFile in theFilesToMove
                set tName to name of thisFile
                if exists item tName in theDestinationFolder then
                      set tContainer to container of thisFile
                      set tExtension to name extension of thisFile
                      if tExtension is not "" then
                            set tName to text 1 thru -((length of tExtension) + 2) of tName
                            set tExtension to "." & tExtension
                      end if
                      if class of thisFile is not alias then set thisFile to thisFile as alias
                      set i to 2
                      repeat -- As you must rename the file before moving it, the name must be unique in his folder and in the destination folder.
                            set tempName to tName & "_" & i & tExtension
                            if not (exists item tempName in theDestinationFolder) and not (exists item tempName in tContainer) then
                                  set name of thisFile to tempName
                                  exit repeat
                            end if
                            set i to i + 1
                      end repeat
                end if
                move thisFile to theDestinationFolder
          end repeat
    
  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    Dec 1, 2012 2:56 AM (in response to MattJayC)

    MattJayC wrote:

     

    Having said that can it be _a _b _c etc as that could suit the workflow better

     

    OK,

     

     

    set tAlpha to "abcdefghijklmnopqrstuvwxyz"
    repeat with thisFile in theFilesToMove
          set tName to name of thisFile
          if exists item tName in theDestinationFolder then
                set tContainer to container of thisFile
                set tExtension to name extension of thisFile
                if tExtension is not "" then
                      set tName to text 1 thru -((length of tExtension) + 2) of tName
                      set tExtension to "." & tExtension
                end if
                if class of thisFile is not alias then set thisFile to thisFile as alias
                if character -2 of tName = "_" and (character -1 of tName) is in tAlpha then -- check underscore
                      set startChar to (id of (character -1 of tName)) - 96
                      if startChar < 1 then set startChar to 1 
                      set tName to text 1 thru -3 of tName
                else
                      set startChar to 1
                end if
                set uScrore to "_"
                repeat -- As you must rename the file before moving it, the name must be unique in his folder and in the destination folder.
                       if startChar > 26 then 
                            set uScrore to uScrore & "_"
                            set startChar to 1
                      end if
                      set tempName to tName & uScrore & (item startChar of tAlpha) & tExtension
                      if not (exists item tempName in theDestinationFolder) and not (exists item tempName in tContainer) then
                            set name of thisFile to tempName
                            exit repeat
                      end if
                      set startChar to startChar + 1
                end repeat
          end if
          move thisFile to theDestinationFolder
    end repeat
    
    

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.