Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Apple脚本,用于将目录中的类似文件夹合并/移动到一个目录中_Javascript_Merge_Applescript_Directory - Fatal编程技术网

Javascript Apple脚本,用于将目录中的类似文件夹合并/移动到一个目录中

Javascript Apple脚本,用于将目录中的类似文件夹合并/移动到一个目录中,javascript,merge,applescript,directory,Javascript,Merge,Applescript,Directory,我有几个文件夹已被拆分成序列,需要合并。例如,一个文件夹序列看起来像“2015A1_lemon_01、2015A1_lemon_02、29A1_lemon_03”,另一个文件夹序列看起来像“Books_01、Books_02、Books_03” 我希望能够制作一个applescript(或者甚至是javascript),它将整个目录中所有类似的文件夹放在一个文件夹中。 序列号不需要保留。如果脚本只需获取每个类似文件夹的内容并将其放入“29A1_lemon”之类的文件夹中,那么这将是理想的 最重要

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

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

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


谢谢你能给我的帮助

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

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

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

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

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

不幸的是,我有太多的文件夹,输入所有的名字会非常耗时。它也许能通过正则表达式得到这些名字吗?它将查找。*?(?=\d+)好的,我发布了另一个答案,在文件名中查找u,并将文件移动到一个新的动态创建的文件夹中。正则表达式匹配将更加理想,因为我希望将脚本用于其他不包含下划线的目录。我更改了另一个答案,使用正则表达式匹配文件。请看答案。好的,我已经更新了脚本,以适应使用正则表达式匹配文件和文件夹名称的情况。这几乎可以做到,但applescript中的正则表达式有什么问题?我试着把你做的那个改成我以前做过的那个,它说应该是“”,但发现了未知的标记…我猜问题是转义字符。你必须转义引号和斜杠之类的东西。因此,如果您的正则表达式是。*?(?=\d+)那么您需要用另一个斜杠来转义斜杠:.*?(?=\\d+)它在这里起作用,所以我所知道要做的就是让您的正则表达式语句正确,包括文件夹名称的()捕获组。也许你需要设置一个测试文件夹/文件来解决这个问题?尽量不要使用特殊代码,如\d。注释掉行:显示对话框f