как использовать applescript для перемещения файлов в новую папку по расширению, сохраняя имя подпапки

Вот что я пытаюсь сделать.

У меня есть файловая структура, которая содержит фотографии в форматах JPG и RAW. Это папка «Фото» с подпапками по дате. Я хотел бы скопировать только фотографии RAW в новую папку «Фотографии RAW», но сохранить структуру по дате съемки / создания.

Я могу скопировать только файлы с помощью automator или applescript из каталога в новый, но как мне пройти по дереву каталогов с помощью applescript, чтобы охватить все подпапки?


person stevebanfield    schedule 13.08.2012    source источник


Ответы (1)


Попробуй это. Вы увидите, что я использовал «все содержимое», чтобы получить файлы во вложенных папках.

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
person regulus6633    schedule 13.08.2012