0

我有一个名为“目录”的文件夹,其中包含大约 1000 个子文件夹。每个子文件夹(“学生”)包含一个或多个子文件夹,其中包含一个或多个文件。是否可以编写一个脚本:

  • 检测子文件夹是否有一个或多个子文件夹
  • 如果“Student”只有一个子文件夹,请将其移动到父目录中名为“Bad”的新文件夹中
  • 如果“Student”有多个子文件夹,请将其移动到父目录中名为“Good”的新文件夹中

显然,因此,我需要“目录”文件夹中的两个文件夹:一个名为“Bad”,其中包含包含一个文件夹的所有文件夹,另一个名为“Good”,其中包含包含多个文件夹的所有文件夹。换句话说,我希望分类法来自:

/Directory/Billy/Grades
/Directory/Billy/Student_Info
/Directory/Bob/Grades
/Directory/Bob/Student_Info  
/Directory/Joe/Student_Info
/Directory/George/Grades

至:

/Directory/Good/Billy/Grades
/Directory/Good/Billy/Student_Info
/Directory/Good/Bob/Grades
/Directory/Good/Bob/Student_Info
/Directory/Bad/Joe/Student_Info
/Directory/Bad/George/Grades
4

1 回答 1

3

在此之前,它使用了一些您将来可以使用的核心 Finder 和 AppleScripting 想法。

请先备份您的数据,以防万一。

tell application "Finder"
    -- Define the full path to your data
    set student_data_folder to folder POSIX file "/Users/Foo/Desktop/bar/students/data"

    -- Get the student folders, ignoring good & bad incase they have already been created
    set all_student_folders to every folder of student_data_folder whose name is not in {"Good", "Bad"}

    --Create the good & bad folders if they don't exist
    set good_folder to my checkFolderExists("Good", student_data_folder)
    set bad_folder to my checkFolderExists("Bad", student_data_folder)

    -- Now loop through all student folders doing the sort based on how many subfolders they have
    repeat with student_folder in all_student_folders
        if (get the (count of folders in student_folder) > 1) then
            -- Its good
            move student_folder to good_folder
        else
            -- It's bad
            move student_folder to bad_folder
        end if
    end repeat

end tell

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

高温高压

于 2013-08-21T01:00:57.553 回答