2017-03-31 55 views
0

我有一個深度爲三級的文件夾,每個級別都包含大量文件。我可以在下面的腳本中獲得兩個級別的深度,但是第三級有一些挑戰。有人會介意提供一些關於我應該如何獲得更深層次的指導嗎?使用Python或其他語言是不可接受的,因爲我試圖看看它如何與AppleScript一起工作。在遞歸處理中深入鑽取

set sourceFolder to (choose folder) 

tell application "Finder" 
    my changeFileNameCase(sourceFolder, "upper") 
    repeat with subFolder in (get every folder of folder sourceFolder) 
     my changeFileNameCase(subFolder as alias, "upper") 

     #This Is No Good 
     repeat with theFolder in (get every folder of folder subFolder) 
      my changeFileNameCase(theFolder, "upper") 
     end repeat 

    end repeat 
end tell 

on changeFileNameCase(targetFolder, caseToSwitchTo) 
    tell application "Finder" 
     set fileList to every file of folder targetFolder 
     repeat with theFile in fileList 
      set oldName to name of theFile 
      set newName to my changeCaseOfText(oldName, caseToSwitchTo) 
      set the name of theFile to newName 
     end repeat 
    end tell 
end changeFileNameCase 

回答

2

要使處理程序遞歸,您必須獲取文件夾的所有項目並檢查每個項目的類。

  • 如果該類folder調用句柄傳遞文件夾
  • 如果該類file其重命名爲

set sourceFolder to (choose folder) 
changeFileNameCase(sourceFolder, "upper") 

on changeFileNameCase(targetFolder, caseToSwitchTo) 
    tell application "Finder" 
     set theList to every item of targetFolder 
     repeat with i from 1 to count theList 
      set theItem to item i of theList 
      if class of theItem is folder then 
       my changeFileNameCase(theItem, caseToSwitchTo) 
      else 
       set oldName to name of theItem 
       set newName to my changeCaseOfText(oldName, caseToSwitchTo) 
       set name of theItem to newName 
      end if 
     end repeat 
    end tell 
end changeFileNameCase 
+0

啊,我幾乎看到你有這個打算,但它看起來像是如果該項目是一個文件夾,這將重命名該文件夾,因爲我只想要重命名文件。我假設那裏我必須建立一個子文件夾列表?如果該項目是一個文件夾 - 建立一個子文件夾列表? – Jesse

+0

不,如果項目是一個文件夾,它會遞歸地調用處理程序。級別的數量並不重要。 – vadian

+0

第二次看,我看到處理程序正在調用自己。無論如何,它拋出一個錯誤調用自己的錯誤「Finder有一個錯誤:無法繼續changeFileNameCase。」號碼-1708我會花一些時間分開這個 – Jesse