使用AppleScript移动和合并文件夹和文件(第2部分,需要帮助修改)

时间:2013-08-26 21:29:10

标签: macos vba applescript taxonomy finder

我使用了以下来自用户adamh的脚本来整合文件夹

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

现在我需要更多帮助。我的分类法是这样的:

/Directory/Bad/Walt/Student_Info/studentPicture1593859.png
/Directory/Bad/Jesse/Student_Info
/Directory/Bad/Hank/Student_Info/studentPicture4675935.png
/Directory/Bad/Skyler/Student_Info
/Directory/Bad/Marie/Student_Info
/Directory/Bad/Flynn/Student_Info
/Directory/Bad/Saul/Student_Info/studentPicture3984834.png

我现在希望“bad”文件夹有两个子文件夹:“HasContent”和“Empty”。我需要帮助,因为它实际上是在两个级别的子文件夹中搜索并确定文件是否存在,而前一个脚本。换句话说,新的分类法应该如下:

/Directory/Bad/HasContent/Walt/Student_Info/studentPicture1593859.png
/Directory/Bad/Empty/Jesse/Student_Info
/Directory/Bad/HasContent/Hank/Student_Info/studentPicture4675935.png
/Directory/Bad/Empty/Skyler/Student_Info
/Directory/Bad/Empty/Marie/Student_Info
/Directory/Bad/Empty/Flynn/Student_Info
/Directory/Bad/HasContent/Saul/Student_Info/studentPicture39848.png

2 个答案:

答案 0 :(得分:1)

我能够修复此脚本。感谢大家的帮助!

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

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

--Create the HasContent & Empty folders if they don't exist
set HasContent_folder to my checkFolderExists("HasContent", student_data_folder)
set Empty_folder to my checkFolderExists("Empty", 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
    repeat with info_folder in student_folder
        if (get the (count of files in info_folder) > 0) then
            -- Its HasContent
            move student_folder to HasContent_folder
        else
            -- It's Empty
            move student_folder to Empty_folder
        end if
    end repeat
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

答案 1 :(得分:1)

您好我对您的脚本进行了快速测试。并且发现如果学生信息文件夹中有多个项目,文件夹或文件,他们将被计算在内。因此脚本会中断。

如果学生信息文件夹包含文件的子文件夹,而学生信息文件夹中没有文件,那么学生信息文件夹将被移动到空文件夹。

我刚试过这个,似乎效果更好。

这个想法是获取学生信息文件夹包含的项目类型。然后检查是否有任何东西不是文件夹..

如果不再需要检查。退出第二次重复,做必要的移动。然后继续进行第一次重复中的下一个文件夹。

希望这有帮助。

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

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

    --Create the HasContent & Empty folders if they don't exist
    set HasContent_folder to my checkFolderExists("HasContent", student_data_folder)
    set Empty_folder to my checkFolderExists("Empty", 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

        #Get every item including within sub folders
        set theContents to kind of entire contents of student_folder

        #set your bool
        set noFiles to false

        #check if there is anything other than folders
        repeat with i from 1 to number of items in theContents

            set this_item to item i of theContents

            if this_item is not equal to "Folder" then

                #set the bool to reflect there are files
                set noFiles to false

                #found a none folder item so no need to continue
                exit repeat
            else

                set noFiles to true
            end if

        end repeat

        #do your moves
        if noFiles then
            move student_folder to Empty_folder
        else
            move student_folder to HasContent_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