Macos 如何使用applescript按扩展名将文件移动到新文件夹,保留子文件夹名称

Macos 如何使用applescript按扩展名将文件移动到新文件夹,保留子文件夹名称,macos,applescript,automator,Macos,Applescript,Automator,这就是我要做的 我有一个包含JPG和RAW格式照片的文件结构。这是一个名为“照片”的文件夹,其中包含按日期排列的子文件夹。我只想将原始照片复制到一个新文件夹“photos RAW”,但保留拍摄/创建日期的结构 我可以使用automator或applescript将一个目录中的文件复制到一个新目录中,但如何使用applescript遍历目录树以覆盖所有子文件夹?试试这个。你会看到我也使用了“全部内容”来获取子文件夹中的文件 set extensionToFind to "raw" set top

这就是我要做的

我有一个包含JPG和RAW格式照片的文件结构。这是一个名为“照片”的文件夹,其中包含按日期排列的子文件夹。我只想将原始照片复制到一个新文件夹“photos RAW”,但保留拍摄/创建日期的结构


我可以使用automator或applescript将一个目录中的文件复制到一个新目录中,但如何使用applescript遍历目录树以覆盖所有子文件夹?

试试这个。你会看到我也使用了“全部内容”来获取子文件夹中的文件

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

试试这个。你会看到我也使用了“全部内容”来获取子文件夹中的文件

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