Aggiunta di suffissi ai nomi dei file in base ai suffissi precedenti

0

Ho una cartella contenente file come

ABC - 2001
DEF
EFG - 2001-2002
HIJ
KLM - 2003
NOP
QRS - 2004

Voglio aggiungere l'anno a tutti i nomi di file che al momento non hanno suffisso dell'anno. Il suffisso deve essere sempre quello dell'ultimo file con suffisso di un anno: EFG contiene 2001 e l'inizio del 2002, quindi HIJ ha bisogno del suffisso 2002.

Come posso fare questo?

    
posta lejonet 05.11.2014 - 19:03
fonte

2 risposte

1

Questo dovrebbe farlo per i nomi dei file che hai fornito. Suppone che la parte del nome sia composta da lettere maiuscole, spazi e trattini. E nel caso di due anni presenti, sono separati da un trattino. Inoltre, presuppone che il primo file contenga un anno (altrimenti non ha nulla da dedurre da esso) e elaborerà ogni file presente nella directory.

Copia e incolla sul Terminale e, se vuoi controllare prima che lo faccia correttamente, aggiungi un echo prima del mv .

for FILENAME in *
do
  # 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" ]
  then
    # file name didn't include year
    mv "$FILENAME" "$FILENAME - $YEAR"
  else
    # file name did include year
    YEAR=$TRIM
  fi
done
    
risposta data 24.11.2014 - 21:26
fonte
1
-- 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 DEFAULT_SUFFIX : "XXXX"
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)
        else
            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)
            else
                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)
    try
        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

Forse non è il modo migliore per farlo, ma non sono un esperto di mele.

    
risposta data 29.11.2014 - 18:42
fonte

Leggi altre domande sui tag