【问题标题】:AppleScript - Move all the files contained in subfolder to a top folderAppleScript - 将子文件夹中包含的所有文件移动到顶层文件夹
【发布时间】:2019-11-24 12:05:52
【问题描述】:

我正在寻找对当前脚本的编辑。我需要将所有文件从子文件夹(递归)移动到一个顶级文件夹,但是如果存在同名文件,请创建一个新的顶级文件夹并继续在那里,这就是我目前所拥有的:

tell application "Finder"
    try
        set Random_name to random number from 100 to 9999
        set theTopFolder to (choose folder)
        set theFiles to a reference to every file of (entire contents of folder theTopFolder)
        set theNewFolder to make new folder at theTopFolder with properties {name:"Flattened Files"}
        move theFiles to theNewFolder
    on error
        set theNewFolder to make new folder at theTopFolder with properties {name:"Flattened Files" & Random_name}
        move theFiles to theNewFolder
    end try


end tell

只是要明确路径的结构不是:

Mainfolder/subfolder/file.xxx 但 Mainfolder/subfolder/sulbfolder2/subfolder3/....100/file.xxx 所以脚本需要递归地工作,但它会在存在同名文件时停止

当存在同名文件时,我的编辑会创建一个新文件夹,其中包含扁平文件+随机数,但是当移动另一个同名文件时,脚本会因错误而停止,而是继续创建新的扁平文件+随机数文件夹。有什么想法吗?

谢谢

【问题讨论】:

  • 所以您想为重复名称创建一个新文件夹,而不是仅仅添加一个后缀?如果你有多个同名的文件或顶级文件夹,你想怎么办?
  • 嘿@red_menace 感谢您的回复。我正在寻找的是文件夹 Flattened Files1、Flattened Files2、Flattened Files3 等等,包含子文件夹的所有文件。当然,如果没有重复,Flattened Files1 应该包含所有文件。是的,重要的是不要触及文件的名称

标签: applescript


【解决方案1】:

您的脚本没有使用递归,它只是让 Finder 获取所有文件。你得到的额外错误是因为你试图再次移动整个文件列表。

一种解决方案是逐步检查文件项,随时测试重复项,并根据需要创建新文件夹。以下脚本只是将重复项移动到添加的文件夹(请注意,错误仍会停止脚本)。我不知道您是如何对文件列表进行排序的,所以我添加了一行以取消注释以继续将文件项移动到添加的文件夹中。

set theTopFolder to (choose folder)
tell application "Finder"
    set theNewFolder to make new folder at theTopFolder with properties {name:"Flattened Files"}
    set theFiles to every file of (entire contents of folder theTopFolder) as alias list
    repeat with aFile in theFiles
        if file ((theNewFolder as text) & (name of aFile)) exists then -- use added folders for duplicates
            set counter to 1
            set done to false
            repeat until done
                set suffix to text -2 thru -1 of ("000000" & counter) -- leading zeros for sorting
                set alternateFolder to (theTopFolder as text) & "Flattened Files" & space & suffix
                tell me to (do shell script "mkdir -p " & quoted form of POSIX path of alternateFolder) -- make new folder as needed
                if file (alternateFolder & ":" & (name of aFile)) exists then -- continue to next one
                    set counter to counter + 1
                else
                    move aFile to folder alternateFolder
                    # set theNewFolder to folder alternateFolder -- uncomment to continue moving here after a duplicate
                    set done to true
                end if
            end repeat
        else
            move aFile to folder (theNewFolder as text)
        end if
    end repeat
end tell

【讨论】:

  • 嗨@red_menace,谢谢你的脚本。您的版本运行良好,只是为了确保我理解正确:如果我希望脚本继续将文件移动到 Fattened Files 01 直到找到新的副本,我需要删除该行的注释吗?
  • 正确。我没有太多要测试的文件,所以我不确定目的是什么或与正常的 Finder 安排一起工作的效果如何(如果没有某种文件顺序,就不能保证排序),但您在原始帖子中提到过这样做。
猜你喜欢
  • 1970-01-01
  • 2011-04-24
  • 1970-01-01
  • 2020-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-09
相关资源
最近更新 更多