我需要重命名子文件夹中的文件,以便数字前缀长度为三位数。
名称模式是: 1 Audio Track.aiff 2 Audio Track.aiff 等...
我试图找出该做什么,但到目前为止只是设法让人头疼。
感谢所有帮助。
PS。我确实找到了这个子程序,但遗憾的是我的编写技巧很难用它。
on add_leading_zeros(this_number, max_leading_zeros)
set the threshold_number to (10 ^ max_leading_zeros) as integer
if this_number is less than the threshold_number then
set the leading_zeros to ""
set the digit_count to the length of ((this_number div 1) as string)
set the character_count to (max_leading_zeros + 1) - digit_count
repeat character_count times
set the leading_zeros to (the leading_zeros & "0") as string
end repeat
return (leading_zeros & (this_number as text)) as string
else
return this_number as text
end if
end add_leading_zeros
答案 0 :(得分:2)
尝试:
on add_leading_zeros(this_number, max_leading_zeros)
return (do shell script "printf \"%0" & max_leading_zeros & "d\"" & this_number)
end add_leading_zeros
set xxx to add_leading_zeros(5, 2)
或包含文字:
on add_leading_zeros(maxPrefix, myString)
set this_number to (do shell script "grep -Eo ^[0-9]* <<< " & quoted form of myString)
set extraZeros to maxPrefix - (length of this_number)
if extraZeros > 0 then
set myNumber to (do shell script "printf \"%0" & extraZeros & "d\"" & this_number)
set myText to myNumber & (do shell script " sed 's/^[0-9]*//' <<< " & quoted form of myString)
end if
end add_leading_zeros
set xxx to "4441 Audio Track.aiff"
set xxx to add_leading_zeros(6, xxx)
答案 1 :(得分:2)
我会选择一个更简单的解决方案:
on add_leading_zeros(this_number, max_leading_zeros)
return text (max_leading_zeros * -1) thru -1 of ("00000000000000000" & this_number)
end add_leading_zeros
答案 2 :(得分:1)
这里我向您展示子例程的用法,我添加了日志语句,以便您可以看到它是如何工作的。希望它有所帮助:
set thisFilename to "1 Audio Track.aiff"
log "thisFilename: " & thisFilename
set numberPrefix to (first word of thisFilename) as number
log "numberPrefix as number: " & numberPrefix
set numberPrefixWithLeadingZeros to my add_leading_zeros(numberPrefix, 2)
log "numberPrefixWithLeadingZeros as text: " & numberPrefixWithLeadingZeros
set newFileName to numberPrefixWithLeadingZeros & " Audio Track.aiff"
log newFileName
-- ADDING LEADING ZEROS: place leading zeros (0001, 023, etc.) before a number
-- if the maximum number of leading zeros is set to 2, then the results will range from 001 to 999, and so on.
on add_leading_zeros(this_number, max_leading_zeros)
set the threshold_number to (10 ^ max_leading_zeros) as integer
if this_number is less than the threshold_number then
set the leading_zeros to ""
set the digit_count to the length of ((this_number div 1) as string)
set the character_count to (max_leading_zeros + 1) - digit_count
repeat character_count times
set the leading_zeros to (the leading_zeros & "0") as string
end repeat
return (leading_zeros & (this_number as text)) as string
else
return this_number as text
end if
end add_leading_zeros
答案 3 :(得分:1)
让我们将您的问题分解为步骤。
首先,您想要从finder中检索文件。现在,让我们假设您选择了一个文件夹,并希望将脚本应用到其附带的文件中。
tell application "Finder"
set theFolder to the selection
set theFiles to every file of item 1 of theFolder
当您获取Finder的选择时,您会得到一个列表,因此第1项。这也使您有机会通过选择多个文件夹并使用重复循环来迭代它们来扩展它。
接下来,我们想要查看每个文件,所以让我们设置一个调用函数的循环,并将当前文件的文件名作为字符串传递给它:
repeat with aFile in theFiles
set originalName to the name of aFile
set newName to my threeDigitPrefix(originalName)
我们调用的子程序非常简单,首先将文件名字符串分开并将其存储在列表中:
set AppleScript's text item delimiters to " "
set splitName to (every text item of originalName) as list
然后我们将检查文件名是否以数字开头,如果不是,则检查该功能。
try
first item of splitName as number
on error
return "FAILED" -- originalName does not start with a number
end try
现在我们将现有前缀分配给变量并检查其长度以确定我们需要向文件名添加多少个零:
set thePrefix to the first item of splitName
if the length of thePrefix is 1 then
set thePrefix to "00" & thePrefix
else if the length of thePrefix is 2 then
set thePrefix to "0" & thePrefix
end if
然后我们将前缀放回包含我们的破碎文件名的列表中,并重新组合它并将其返回到调用它的循环:
set the first item of splitName to thePrefix
return splitName as string
最后,我们检查函数是否没有失败,并使用我们刚刚从函数中获取的字符串重命名该文件:
if newName is not "FAILED" then
set the name of aFile to newName
end if
我们已经完成了。把它们放在一起,最后得到这个:
tell application "Finder"
set theFolder to the selection
set theFiles to every file of item 1 of theFolder
repeat with aFile in theFiles
set originalName to the name of aFile
set newName to my threeDigitPrefix(originalName)
if newName is not "FAILED" then
set the name of aFile to newName
end if
end repeat
end tell
on threeDigitPrefix(originalName)
set AppleScript's text item delimiters to " "
set splitName to (every text item of originalName) as list
try
first item of splitName as number
on error
return "FAILED" -- originalName does not start with a number
end try
set thePrefix to the first item of splitName
if the length of thePrefix is 1 then
set thePrefix to "00" & thePrefix
else if the length of thePrefix is 2 then
set thePrefix to "0" & thePrefix
end if
set the first item of splitName to thePrefix
return splitName as string
end threeDigitPrefix
答案 4 :(得分:1)
您也可以使用shell脚本:
for f in *.aif; do mv "$f" "$(printf %03d "${f%% *}") ${f#* }"; done
这将搜索当前文件夹下的所有文件:
IFS=$'\n'; for f in $(find "$PWD" -name '*.aif'); do folder=${f%/*}; file=${f##*/}; mv "$f" "$folder/$(printf %03d "${file%% *}") ${file#* }"; done
%%
从最后删除最长的模式,#
从头开始删除最短的模式IFS=$'\n'
将输入字段分隔符设置为换行符,而不是空格,制表符和换行符答案 5 :(得分:0)
我更改了脚本以适应工作流程的变化。我现在正在重命名mp3文件而不是aif文件类型,而且我在新文件名中为父文件夹名称加前缀。
脚本如下所示:
IFS=$'\n'; for f in $(find "$PWD" -name '*.mp3'); do folder=${f%/*}; file=${f##*/}; mv "$f" "$folder/${folder##*/}$(printf %03d "${file%% *}") ${file#* } " ;done
但是我遇到了麻烦。如果文件夹包含10个以上的文件,似乎存在问题(在某些情况下!)。
我使用两种不同的文件类型.docx文件和.mp3文件设置了两个测试用例。测试用例基本上是10个子文件夹,每个子文件夹有10个文件。这些文件根据模式命名:N Audio Track.xxx并按顺序编号为1-10。我似乎得到了一些奇怪的结果。在.docx文件的情况下,我得到了正确的结果,但如果我使用mp3文件设置完全相同的文件夹和文件结构,我会在10个以上文件的所有文件夹中得到奇怪的结果。我得到一个文件重命名为000 Audio Track.mp3这很奇怪,而且应该是008和009的文件也不存在。对于可能导致这种情况的原因,我完全不知道。