首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apple脚本将目录中的类似文件夹合并/移动到一个目录中

问Apple脚本将目录中的类似文件夹合并/移动到一个目录中
EN

Stack Overflow用户
提问于 2015-10-12 19:59:50
回答 2查看 319关注 0票数 1

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

我希望能够制作一个applescript (或者甚至是javascript),它可以将整个目录中的所有类似文件夹都放入一个目录中。

不需要保留序列号。如果脚本只需获取每个类似文件夹的内容,并将其放入像"29A1_lemon“这样的文件夹,那就太理想了。

最主要的是,所有的文件夹内容都会被移动到相应的文件夹中--脚本并不需要真正地合并所有内容,尽管这会很有帮助。此外,脚本能够查看多个不同的文件夹,而不是一次只查看一个序列,这一点至关重要。

感谢您的任何帮助!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-13 04:49:06

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

更新:我更新了脚本以隔离regex匹配的。

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2015-10-12 20:51:37

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

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33089242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档