将文件拆分到文件夹中

时间:2014-02-20 23:11:07

标签: macos applescript automator

有人请让我开始使用Automator和/或AppleScript。我对此感到非常沮丧。我想以预定的顺序(可能按名称或按日期)获取一个非常大的文件夹(数千个)并将它们移动到子文件夹中,每个子文件夹不超过指定的大小(可能是4.7GB)。我不希望ISO或DMG或任何只是我的文件很好地分成磁盘大小块。也没有重新调整订单。如果一个磁盘只适合一个10MB的文件,因为下一个文件会超出限制,那么就这样吧。没有文件会超过限制,以防你想知道 - 它们将达到约50MB的顶部。

到目前为止,我已经获得了一个文件夹操作,其中包含获取选定的Finder项目,然后是AppleScript

on run {input, parameters}
    return first item of input
end run

这让我成为第一个项目。我可以创建一个文件夹磁盘1也是。我也可以移动文件。但是,如何确定是否要移动到此文件夹或是否需要创建新文件夹?

如果可能,我想在Automator中这样做,但怀疑我需要一点AppleScript。我确定这个问题已经解决了,所以如果可以的话,请联系我。感谢。

1 个答案:

答案 0 :(得分:0)

这是一个有效的AppleScript。这不完美,但完成工作。

有很多文件可能会很慢,我相信它可以改进。高性能不是Applescript的优势之一。

tell application "Finder"
    set files_folder to folder POSIX file "/Users/MonkeyMan/Desktop/MyMessOfFiles"
    set destination_folder to folder POSIX file "/Users/MonkeyMan/Desktop/SortedFiles"

    set target_size to 250.0 --   size limit your after in MB.

    set file_groupings to {} -- list we will store our stuff in
    set current_files to {} -- list of the current files we are adding size 

    set total_files to the count of files in files_folder

    set current_size_count to 0

    repeat with i from 1 to total_files
        set file_size to (the size of file i of files_folder) / 1024.0 / 1024.0 -- convert to MB to help prevent overrunning what the variable can hold

        if ((current_size_count + file_size) ≥ target_size) then
            -- this will overrun our size limit so store the current_files and reset the counters
            set the end of file_groupings to current_files
            set current_files to {}
            set current_size_count to 0
        end if

        set current_size_count to current_size_count + file_size
        set the end of current_files to (a reference to (file i of files_folder) as alias)

        if (i = total_files) then
            -- its the last file so add the current files disreagarding current size
            copy current_files to the end of file_groupings
        end if

    end repeat

    -- Copy the files into sub folders
    set total_groups to the count of file_groupings
    repeat with i from 1 to total_groups
        set current_group to item i of file_groupings
        set dest_folder to my ensureFolderExists(("disk " & i), destination_folder)
        move current_group to dest_folder
    end repeat

end tell

say "finished"

on ensureFolderExists(fold_name, host_folder)
    tell application "Finder"
        if not (exists folder fold_name of host_folder) then
            return make new folder at host_folder with properties {name:fold_name}
        else
            return folder fold_name of host_folder
        end if
    end tell
end ensureFolderExists