Question

I have a folder called "Directory" with about 1000 subfolders. Each subfolder ("Student") contains one or more subfolders, which has one or more files. Is it possible to write a script that:

  • Detects whether the subfolder has one or more than one subfolder
  • If "Student" has ONLY one subfolder, move it into a new folder called "Bad" in the parent directory
  • If "Student" has MORE THAN ONE subfolder, move it into a new folder called "Good" in the parent directory

Obviously, as a result, I need two folders in the "Directory" folder: one called "Bad", which contains all the folders containing one folder, and one called "Good", which contains all the folders containing more than one folder. In other words, I want the taxonomy to go from:

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

To:

/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
Était-ce utile?

La solution

Give this ago, it uses a few core Finder and AppleScripting ideas you can use in the future.

Please make a backup of your data first, just in case.

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

HTH

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top