【问题标题】:Applescript - move files to folder which name begins with first 2 characters of filenameApplescript - 将文件移动到名称以文件名的前 2 个字符开头的文件夹
【发布时间】:2014-02-15 17:21:53
【问题描述】:

我需要一些帮助来微调这个脚本。是否可以让此脚本根据文件的前两个字母匹配子文件夹?父文件夹将是“Shots”,其中包含具有唯一两个字母前缀“BA_Bikini_Atol”的子文件夹,其中包含这些文件夹中用于特定镜头 ba_0020、ba_0030 等的子文件夹。想要通过选择“shots”将文件 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

【问题讨论】:

  • 不完全重复,但这个问题显示了如何根据文件名的开头移动文件:stackoverflow.com/questions/18397954/…
  • Darrick - 这是我正在使用的脚本,效果很好,但是如何让脚本查找子文件夹以进行匹配?

标签: applescript


【解决方案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

来自:

到:

【讨论】:

  • markhunte。谢谢你。一个后续问题。如果我想在目标文件夹中为所有 .mov 文件再添加一个名为“qts”的文件夹,该怎么办。关于如何做到这一点的任何想法?
【解决方案2】:

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

【讨论】:

  • 谢谢sirbaxx。我的系统软件是 OSX 10.9.1。我了解 ASObjC Runner 仅支持最高 10.9 有没有办法在没有 ASObjC 帮助器的情况下将此脚本保留在 Apple Script 中?
  • 我认为它也将在 10.9.1 上运行。你试过了吗?该脚本只需要 ASObjC Runner 提供的 applescript 命令
  • @Mull 我对脚本进行了更正,并在 10.9.1 上对其进行了测试。有用。如果要移动的文件与要移动到的子文件夹位于同一文件夹中,则必须删除 mgFilesFolder 的 with recursion
  • 再次感谢 sirbaxx。接近了,但是有一个问题。我的源文件夹中有多个文件具有相同的前缀,即:ba_0020_v0002、ba_0020_v0003、ba_0020_v0023 等。这是脚本中断的地方。如果我从源文件夹中删除所有倍数,则脚本有效。想法?
猜你喜欢
  • 2013-08-26
  • 1970-01-01
  • 2021-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
相关资源
最近更新 更多