2012-08-13 139 views
1

以下是我正在嘗試執行的操作。如何使用applescript通過擴展將文件移動到新文件夾,保留子文件夾名稱

我有一個文件結構,其中包含JPG和RAW格式的照片。這是一個名爲「Photos」的文件夾,帶有按日期排列的子文件夾。我只想將RAW照片複製到一個新的文件夾「Photos RAW」中,但是保留該結構的拍攝/創建日期。

我可以只使用automator或applescript目錄將文件複製到新的目錄,但是如何使用applescript來遍歷目錄樹,以便覆蓋所有子文件夾?

回答

2

試試這個。你會看到我使用「全部內容」來獲取子文件夾中的文件。

set extensionToFind to "raw" 

set topLevelFolder to (choose folder) as text 
set pathCount to count of topLevelFolder 

tell application "Finder" 
    -- get the files 
    set rawFiles to files of entire contents of folder topLevelFolder whose name extension is extensionToFind 
    if rawFiles is {} then return 

    -- setup the folder where the files will be moved 
    set rawFolder to ((container of folder topLevelFolder) as text) & "Photos_Raw:" 
    do shell script "mkdir -p " & quoted form of POSIX path of rawFolder 

    repeat with aFile in rawFiles 
     set aFileContainer to (container of aFile) as text 
     if topLevelFolder is equal to aFileContainer then 
      -- here the file is at the top level folder 
      set newPath to rawFolder 
     else 
      -- here we calculate the new path and make sure the folder structure is in place 
      set thisFile to aFile as text 
      set subFolderPath to text (pathCount + 1) thru -((count of (get name of aFile)) + 1) of thisFile 
      set newPath to rawFolder & subFolderPath 
      do shell script "mkdir -p " & quoted form of POSIX path of newPath 
     end if 

     move aFile to folder newPath 
    end repeat 
end tell 
相關問題