【问题标题】:Apple script to merge/move similar folders in a directory into oneApple脚本将目录中的相似文件夹合并/移动到一个
【发布时间】:2015-10-12 19:59:50
【问题描述】:

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

我希望可以制作一个applescript(甚至可能是javascript),它将整个目录中的所有类似文件夹都放在一个目录中。
序列号不需要保留。如果脚本可以只获取每个类似文件夹的内容并将其放入诸如“29A1_lemon”之类的文件夹中,那将是理想的

主要是所有文件夹内容都被移动到适当的文件夹 - 脚本不必逐字合并所有内容,尽管这会有所帮助。此外,脚本可以查看多个不同的文件夹,而不仅仅是一个序列,这一点也很重要。

感谢您提供的任何帮助!

【问题讨论】:

    标签: javascript merge applescript directory


    【解决方案1】:

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

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

    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
    

    【讨论】:

    • 好的,我已经更新了脚本以适应使用正则表达式来匹配文件和文件夹名称。
    • 几乎可以做到,但是applescript中的RegEx有什么问题?我尝试将您制作的那个更改为我之前的那个,它说预期为“”,但发现未知令牌...
    • 我猜问题是转义字符。您必须转义引号 " 和正斜杠 \。因此,如果您的正则表达式是 .*?_(?=\d+) 那么您需要用另一个斜杠转义斜杠:.*?_(?=\\ d+)
    • 它在这里工作,所以我所知道的就是让您正确地处理正则表达式语句,包括文件夹名称的 () 捕获组。也许您需要设置一个测试文件夹/文件来解决这个问题?尽量不要使用像 \d 这样的特殊代码。
    • 注释掉该行:显示对话框 f
    【解决方案2】:

    此脚本将递归移动所有文件。首先提供要在各个文件中查找的文件夹名称列表会让您有负担。

    property foldertoSearch : "Macintosh HD:Users:myHome:bigFolder:"
    property destinationFolder : "Macintosh HD:Users:myHome:"
    property folderNames : {"29A1_lemon", "Books_"}
    
    tell application "Finder"
        -- create destination folders
        repeat with f in folderNames
            if not (exists alias (destinationFolder & f & ":")) then
                make new folder at destinationFolder with properties {name:f}
            end if
        end repeat
    
        -- get all files recursively within the search folder
        set oldFileList to (files of entire contents of alias foldertoSearch)
        repeat with thisFile in oldFileList
            set n to name of thisFile
            repeat with f in folderNames
                if f is in n then
                    move thisFile to folder (destinationFolder & f & ":")
                end if
            end repeat
        end repeat
    end tell
    

    【讨论】:

    • 不幸的是,我有这么多文件夹,输入所有名称会非常耗时。它是否可以通过 ReGex 获得名称?它会寻找 .*?_(?=\d+)
    • 好的,我发布了另一个答案,在文件名中查找 _ 并将文件移动到新的动态创建的文件夹
    • 正则表达式匹配会更理想,因为我希望将脚本用于其他不包含下划线的目录。
    • 我更改了另一个答案以使用 RegEx 匹配文件。请看那个答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多