苹果脚本将merge/move个目录下的相似文件夹合二为一
Apple script to merge/move similar folders in a directory into one
我有几个文件夹被拆分成序列,需要合并。例如,一个文件夹序列看起来像“2015A1_lemon_01、2015A1_lemon_02、29A1_lemon_03”,另一个序列像 "Books_01, books_02, books_03"。
我希望的是可以制作一个 applescript(或者甚至 javascript),它可以获取 entire 目录中的所有类似文件夹并将所有其内容合二为一。
序列号不需要保留。如果脚本能把每个类似的文件夹的内容都放到一个文件夹里,比如“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 "" -- [=10=] whole match, first expression capture group in parentheses, 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
我有几个文件夹被拆分成序列,需要合并。例如,一个文件夹序列看起来像“2015A1_lemon_01、2015A1_lemon_02、29A1_lemon_03”,另一个序列像 "Books_01, books_02, books_03"。
我希望的是可以制作一个 applescript(或者甚至 javascript),它可以获取 entire 目录中的所有类似文件夹并将所有其内容合二为一。
序列号不需要保留。如果脚本能把每个类似的文件夹的内容都放到一个文件夹里,比如“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 "" -- [=10=] whole match, first expression capture group in parentheses, 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