2014-02-12 41 views
0

我想要一些帮助来微调此脚本。该脚本是否可以匹配基于文件头两个字母的子文件夹?父文件夹将是“镜头”,其中包含具有唯一的两个字母前缀“BA_Bikini_Atol”的子文件夹,其中包含用于特定镜头ba_0020,ba_0030等的文件夹内的子文件夹。想要通过选择“镜头”将文件ba_0020_v0002移动到ba_0020文件夹“作为目标,脚本通过所有子文件夹查找匹配项。思考?Applescript - 将文件移动到文件夹名称以文件名的前2个字符开头的地方

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 
+0

不完全重复,但这个问题说明了如何基于文件名的开头移动文件:http://stackoverflow.com/questions/18397954/applescript-move-files -to-folder-which-name-starts-with-first-7-characters-of/ –

+0

Darrick - 这是我正在使用的脚本,效果很好,但是如何让脚本查看子文件夹比赛? – Mull

回答

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

+0

markhunte。谢谢。一个跟进问题。如果我想为所有.mov文件在目标文件夹“qts”中添加一个以上的文件夹,该怎么办?有关如何做到这一点的任何想法? – Mull

0

我已经(在本网站的用户的帮助下)您所指的脚本。
从那时起,我制作了类似(更高效)的脚本。他们中的大多数依赖于ASObjC亚军,你可以在这里: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 
+0

谢谢sirbaxx。我的系统软件是OSX 10.9.1。我知道ASObjC Runner最多只支持10.9是否有一种方法可以在没有ASObjC帮助器的Apple Script中保存此脚本? – Mull

+0

我认为它也会在10.9.1上运行。你有没有尝试过呢。该脚本只需要ASObjC Runner提供的applescript命令 – sirbaxx

+0

@Mull我已经对该脚本进行了更正并在10.9.1上对其进行了测试。有用。如果要移动的文件与要移动到的子文件夹位于同一文件夹中,则必须删除mgFilesFolder – sirbaxx

相关问题