javascript - Apple脚本将目录中的相似文件夹合并/移动到一个目录中

标签 javascript merge applescript directory

我有几个文件夹已被拆分成序列,需要合并。例如,一个文件夹序列看起来像“2015A1_lemon_01、2015A1_lemon_02、29A1_lemon_03”,另一个序列像“Books_01、books_02、books_03”。

我希望的是可以制作一个 applescript(或者甚至是 javascript),它将整个目录中的所有类似文件夹放入一个目录中。
序列号不需要保留。如果脚本可以将每个类似文件夹的内容放入例如“29A1_lemon”这样的文件夹中,那将是理想的

最主要的是所有文件夹内容都被移动到适当的文件夹 - 脚本不必真正合并所有内容,尽管这会有所帮助。同样重要的是,脚本可以查看多个不同的文件夹,而不是一次只查看一个序列。

感谢您提供的任何帮助!

最佳答案

此脚本将递归地检查所有文件,对于与文件名中规定的正则表达式匹配的每个文件,它将将该文件移动到名称与正则表达式搜索中括号中的第一个捕获组匹配的文件夹。

更新:我已更新脚本以隔离正则表达式匹配

use framework "Foundation"
use scripting additions

property foldertoSearch : "Macintosh HD:Users:jweaks:Desktop:test:" --myHome:BigFolder:"
property destinationFolder : "Macintosh HD:Users:jweaks:"
property fileRegex : "(.+)_.+" -- checks file name for this pattern, place folder name in a (capture group)

tell application "Finder"
    -- get all files recursively within the search folder
    set oldFileList to (files of entire contents of alias foldertoSearch)
    -- prep the item delimiter to detect potential folder name within file names
    set otid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "_"

    repeat with thisFile in oldFileList
        set n to name of thisFile
        -- files containing a _ will be moved

        set f to my getMatch(n, fileRegex)
        display dialog f
        if f is not "" then
            -- create folder if not already exists
            if not (exists alias (destinationFolder & f & ":")) then make new folder at destinationFolder with properties {name:f}
            -- move the file
            move thisFile to folder (destinationFolder & f & ":")
        end if
    end repeat
    -- restore the old item delimiter
    set AppleScript's text item delimiters to otid
end tell

on getMatch(theString, toMatch)
    -- theString = string to search
    -- toMatch = regex to test if found in the string
    set returnString to "$1" -- $0 whole match, $1 first expression capture group in parentheses, $2 etc.

    set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:toMatch options:0 |error|:(missing value)


    set isMatch to theRegEx's numberOfMatchesInString:theString options:0 range:{location:0, |length|:length of theString} --NSMakeRange(0, [string length])]

    if isMatch > 0 then

        set theResult to theRegEx's stringByReplacingMatchesInString:theString options:0 range:{location:0, |length|:length of theString} withTemplate:returnString

        return theResult as text
    else
        return ""
    end if
end getMatch

关于javascript - Apple脚本将目录中的相似文件夹合并/移动到一个目录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33089242/

相关文章:

vba - 如何使用 Apple 脚本访问 Powerpoint(Mac 版)的对象事件?

javascript - 操作json数据和简单的for语句

ruby - 惯用 ruby : data structure transformation

automation - 使用脚本将 ttf/woff/woff2 文件重命名为 PostScript 字体名称

scripting - 如何使用 AppleScript 关闭 Safari 中的所有或部分选项卡?

r - 如何合并多个具有共同主题 ID 列和其他列中许多不同变量的 CSV 文件?内R

javascript - 如何使用下拉框显示表格

javascript - 在 ember/handlebars 中使用 value 和 valueBinding 有什么区别?

javascript - HTML 5 画笔颜色不会改变值

python - 如何根据条件合并两个不同大小的 Pandas DataFrame