Applescript - 将文件移动到名称以文件名的前2个字符开头的文件夹

时间:2014-02-12 06:46:18

标签: applescript

我想帮助微调一下这个脚本。是否可以根据文件的前两个字母使此脚本匹配子文件夹?父文件夹将是“镜头”,其中包含具有唯一两个字母前缀“BA_Bikini_Atol”的子文件夹,其中包含特定镜头ba_0020,ba_0030等文件夹中的子文件夹。想通过选择“镜头”将文件ba_0020_v0002移动到ba_0020文件夹“作为目标,脚本会查看所有子文件夹以查找匹配项。想法?

set mgFilesFolder to (choose folder with prompt "Where are the files stored which you would like to move to the destination?")
set mgDestFolder to (choose folder with prompt "Where is the destination folder?")

tell application "System Events"
    set folderList to name of folders of mgDestFolder
    set fileList to name of files of mgFilesFolder
end tell

repeat with i from 1 to (count folderList)
    set folderName to item i of folderList
    set filesToMove to {}
    repeat with j from 1 to (count fileList)
        set filename to item j of fileList
        if filename begins with folderName then
            set end of filesToMove to alias ((mgFilesFolder as string) & filename)
        end if
    end repeat

    tell application "Finder"
        move filesToMove to alias ((mgDestFolder as string) & folderName & ":")
    end tell
end repeat

2 个答案:

答案 0 :(得分:1)

这适用于我的测试

     set mgFilesFolder to (choose folder with prompt "Where are the files stored which you would like to move to the destination?")
        set mgDestFolder to (choose folder with prompt "Where is the destination folder?")


        (* get the files of the mgFilesFolder folder *)
    tell application "Finder" to set fileList to files of mgFilesFolder

    (* iterate over every file *)
    repeat with i from 1 to number of items in fileList
    set this_item to item i of fileList

    set this_file_Name to name of this_item as string

    (* get the file name code
    Using the delimiter "_"  break the name into fields of text and returning fields 1 thru 2 .   i.e ba_0030 *)
    set thisFileCode to (do shell script "echo " & quoted form of this_file_Name & " |cut -d  _ -f 1-2")
    log thisFileCode
    tell application "Finder"
        set folderList to ((folders of entire contents of mgDestFolder) whose name starts with thisFileCode)
        if folderList is not {} then

            move this_item to item 1 of folderList
        end if
    end tell
end repeat

FROM:

enter image description here

TO:

enter image description here

答案 1 :(得分:0)

我(在本网站用户的帮助下)提到了你所指的剧本 从那以后,我制作了类似(更有效)的脚本。他们中的大多数都依赖于 ASObjC Runner ,您可以访问:Download ASObjC Runner

如果安装了它,我认为以下脚本可以正常工作:

set mgFilesFolder to (choose folder with prompt "Where are the file stored which you would like to move?")

set mgDestFolder to (choose folder with prompt "Where are destination folders?")

tell application "ASObjC Runner"

set mgFiles to enumerate folder mgFilesFolder with recursion without including folders
repeat with mgFile in mgFiles
    set mgPrefix to text 1 thru 7 of name of (about file mgFile include only "name")
    set mgDest to enumerate folder mgDestFolder match prefix mgPrefix with recursion
    manage file mgFile moving into mgDest
end repeat
end tell