2013-02-26 64 views
3

我需要重命名子文件夾內的文件,以便數字前綴長度爲三位數。Applescript將前導零添加到文件名,如何?

名稱模式是: 1音頻Track.aiff 2音頻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 

回答

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 

最後,我們檢查功能並沒有失敗,該文件與ST重命名戒指,我們剛剛從功能中獲得:

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 
+0

這是我需要讓我的頭圍繞這件事的教訓。 Applescript不像我以前用過的任何其他東西,所以非常感謝你對這種真正發生的事情有這樣的瞭解。你先生......太棒了! – Nordanfors 2013-02-26 14:28:11

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 
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

你也可以只使用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'設置輸入字段分隔符來換行,而不是空格,製表最短的模式,換行
+0

謝謝!這當然是一個非常優雅的解決方案,可能會更有效。這個腳本不會通過子文件夾遞歸地操作嗎?如果不是,我將如何重命名所選文件夾的所有子文件夾中的所有文件? – Nordanfors 2013-02-27 08:23:00

+0

@ user1047256我編輯了答案。如果你有不以數字開頭的aif文件,它不起作用。 – user495470 2013-02-27 09:15:22

+0

與使用Applescript相比,這非常快速,並且完美運行。十分感謝! – Nordanfors 2013-02-27 09:37:27

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的文件不在那裏。對於可能導致這種情況的原因,我完全無能爲力。

+0

難道你覺得這似乎並不好笑嗎?只要你尋求幫助,你就可以自己解決問題了嗎?我發現了這個罪魁禍首,事實證明,我是罪魁禍首。我曾在不知不覺中在某些文件中添加了一個前導零,並且直到我休息一會兒,然後才以新鮮的眼睛回來。咄! – Nordanfors 2013-02-27 15:29:37

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