I have a folder containing files like

ABC - 2001
EFG - 2001-2002
KLM - 2003
QRS - 2004

I want to add the year to all file names that currently have no year suffix. The suffix should always be the one of the last file with a year suffix: EFGcontains 2001 and the start of 2002, so HIJ needs the suffix 2002.

How could I do this?


This should do it for the file names you have given. It assumes that the name part consists of uppercase letters, spaces, and dashes. And in the case of two years present, that they are separated by a dash. Also, it assumes that the first file does contain a year (otherwise it has nothing to deduct it from), and it will process every file present in the directory.

Copy&paste to the Terminal, and if you want to check first that it will do it correctly, add an echo before the mv.

for FILENAME in *
  # remove name part
  TRIM=`echo $FILENAME | sed -E 's/[A-Z -]*//'`
  # remove possible start year
  TRIM=`echo $TRIM | sed -E 's/[0-9]*-//'`
  if [ -z "$TRIM" ]
    # file name didn't include year
    # file name did include year
| improve this answer | |
-- open a finder window to allow the user to select the target folder
-- a logfile will be created on the desktop
-- set PROCESS_RENAME to true to disable demo mode !!!!

property PROCESS_RENAME : false
property DIGITS : {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
property LOGFILE : "~/Desktop/renaming.log"

tell application "Finder"
    set currentSuffix to DEFAULT_SUFFIX

    -- let the user select the target folder
    set targetFolder to (choose folder with prompt "Select the target folder:")

    display dialog "The following folder files will be renamed: " & linefeed & POSIX path of targetFolder
    my logfunc(linefeed & "New folder renaming on folder: " & POSIX path of targetFolder & linefeed)

    -- get the target folder files
    set folderFiles to name of every file of entire contents of targetFolder

    -- iterate on each file found
    repeat with targetFile in folderFiles
        set fileExtension to ""
        set fileBaseName to targetFile

        -- extract file base name and file extension
        -- expect file extension to be something like ".XXX"
        if (targetFile contains ".") then
            -- point found so extract base name and extension
            set fileExtension to (characters -4 thru -1 of (targetFile as text)) as text
            set fileBaseName to (characters 1 thru -5 of (targetFile as text)) as text
        end if

        -- extract suffix from base file or filename if no extension found
        set newSuffix to extract_year_suffix(fileBaseName) of me

        -- suffix found on the new file ?
        if (length of newSuffix is greater than 0) then
            set currentSuffix to newSuffix
            set msg to ("FileName: " & targetFile & " >> No name change!")
            my logfunc(msg)
            set newFileName to (fileBaseName & " - " & currentSuffix & fileExtension)

            if (PROCESS_RENAME) then
                -- rename file via shell script
                set sourcePath to (quoted form of (POSIX path of targetFolder & targetFile))
                set destPath to (quoted form of (POSIX path of targetFolder & newFileName))

                my rename_file(sourcePath, destPath)
                set msg to ("DEMO: Old Name: " & targetFile & " >> New Name: " & newFileName)
                my logfunc(msg)
            end if
        end if
    end repeat
    display dialog "End of renaming folder: " & linefeed & POSIX path of targetFolder
end tell

to extract_year_suffix(fileBaseName)
    set found_suffix to ""
    -- check presence of year suffix
    if fileBaseName contains "-" then

        -- extract the last 4 chars 
        set theLast4Chars to (characters -4 thru -1 of fileBaseName)
        -- check all the last 4 Chars are digits
        set allAreDigits to true
        repeat with aChar in theLast4Chars
            if aChar is not in DIGITS then
                set allAreDigits to false
                exit repeat
            end if
        end repeat

        if allAreDigits then
            set found_suffix to (theLast4Chars as text)
            return found_suffix
        end if
    end if
    -- no year suffix found
    return ""
end extract_year_suffix

on logfunc(msg)
    do shell script "echo '" & msg & "' >> " & LOGFILE
end logfunc

on rename_file(sourcePath, destPath)
        set shellCommand to "mv -f " & sourcePath & " " & destPath
        my logfunc(shellCommand)
        do shell script shellCommand
    on error line number num
        display dialog "rename_file: Error on line number " & num
    end try
end rename_file

Perhaps not the best way to do it, but i'm not an applescript expert.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .