gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:32:52 25 4
gpt4 key购买 nike

我有几个文件夹已被拆分成序列,需要合并。例如,一个文件夹序列看起来像“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com