AppleScript-将子文件夹中包含的所有文件移动到顶部文件夹

AppleScript-将子文件夹中包含的所有文件移动到顶部文件夹,applescript,Applescript,我正在寻找对当前脚本的编辑。我需要的是将所有文件从子文件夹(递归)移动到一个顶部文件夹中,但是如果存在同名文件,请创建一个新的顶部文件夹并继续,这就是我到目前为止所做的: tell application "Finder" try set Random_name to random number from 100 to 9999 set theTopFolder to (choose folder) set theFiles to a re

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

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,因此脚本需要递归工作,但当存在同名文件时,脚本会停止

当存在同名文件时,“我的编辑”将使用展平文件+随机数创建一个新文件夹,但是当移动另一个同名文件时,脚本会因错误而停止,而不是继续创建一个新的展平文件+随机数文件夹。有什么想法吗


谢谢

您的脚本没有使用递归,它只是让查找程序获取所有文件。您遇到的另一个错误是,您试图再次移动整个文件列表

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

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_meane谢谢你的回复。我要查找的是文件夹展平文件1、展平文件2、展平文件3等等,其中包含子文件夹的所有文件。当然,如果没有重复文件,扁平文件1应该包含所有文件。是的,重要的是不要触摸文件名hi@red_nerge,谢谢你的脚本。您的版本运行良好,只是为了确保我正确理解:如果我希望脚本继续将文件移动到胖文件01,直到找到新的副本,我需要删除该行的注释?正确。我没有那么多的文件要测试,所以我不确定它的目的是什么,也不确定它在正常的查找器安排下能起到多大的作用(如果没有某种排序,就不能保证文件的顺序),但是你在最初的帖子中提到了这样做。