【问题标题】:Splitting files into folders将文件拆分到文件夹中
【发布时间】:2014-02-22 19:52:05
【问题描述】:

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

到目前为止,我有一个文件夹操作,其中包含 Get Selected Finder Items,然后是这个 AppleScript

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

这让我得到了第一个项目。我可以创建一个文件夹,例如磁盘 1 也是。我也可以移动文件。但是如何确定是移动到该文件夹​​还是需要创建一个新文件夹?

如果可能,我想在 Automator 中执行此操作,但我怀疑我需要一点 AppleScript。我确定这个问题已经解决了,所以如果可以的话,请给我链接。谢谢。

【问题讨论】:

  • 是的,很公平。但我现在已经装满了盘子,希望有人能花几分钟时间给我一些可以继续做的东西的外壳。
  • 我是一名 Windows 用户。 Mac 不是我的强项。我很坏。
  • 当您说 Actionscript 时,您实际上是指 Actionscript,还是指 Applescript?另外,也许命令行/终端是要走的路。
  • 是的,我想我的意思是 AppleScript,抱歉。
  • 你会说我应该使用 bash 吗?

标签: macos applescript automator


【解决方案1】:

这是一个可以执行此操作的有效 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2023-03-15
    • 2021-06-02
    • 1970-01-01
    • 2014-08-14
    • 2015-04-20
    • 1970-01-01
    相关资源
    最近更新 更多