답변:
당신이 지정한 파일 이름에 대해 그렇게해야합니다. 이름 부분은 대문자, 공백 및 대시로 구성되어 있다고 가정합니다. 그리고 2 년이 존재하는 경우, 그것들은 대시로 분리됩니다. 또한 첫 번째 파일에 연도가 포함되어 있지 않다고 가정하고 (그렇지 않으면 공제 할 것이 없습니다) 디렉토리에있는 모든 파일을 처리합니다.
터미널에 복사하여 붙여 넣기를하고, 제대로 수행되는지 먼저 확인하려면 앞에을 추가 echo
하십시오 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
-- 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
아마도 최선의 방법은 아니지만, 저는 애플 스크립트 전문가가 아닙니다.