3

我需要重命名子文件夹中的文件,使数字前缀长度为三位数。

名称模式是:1 Audio Track.aiff 2 Audio Track.aiff 等...

我试图弄清楚该怎么做,但到目前为止,我只是头疼得厉害。

感谢所有帮助。

附言。我确实找到了这个子例程,但遗憾的是我的脚本技能无法很好地利用它。

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
4

6 回答 6

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)
于 2013-02-26T12:36:40.113 回答
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
于 2013-02-28T13:41:01.297 回答
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
于 2013-02-26T12:27:36.373 回答
1

让我们将您的问题分解为几个步骤。

首先,您要从查找器中检索文件。现在,假设您选择了一个文件夹并希望将脚本应用到其随附的文件中。

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
于 2013-02-26T12:43:38.120 回答
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'将输入字段分隔符设置为换行符,而不是空格、制表符和换行符
于 2013-02-26T15:51:45.213 回答
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 的文件也不存在。我完全不知道可能导致这种情况的原因。

于 2013-02-27T15:04:30.283 回答