Reputation: 52
I have several folders that have been split into sequences and need to be merged. For example, one sequence of folders looks like "2015A1_lemon_01, 2015A1_lemon_02, 29A1_lemon_03" and another sequence like "Books_01, books_02, books_03".
what I'm hoping is that an applescript (or perhaps even javascript) can be made which takes all the similar folders in the entire directory and puts all its contents into one.
The sequence number does not need to be preserved. If the script can just take the contents of every similar folder and put it into a folder like "29A1_lemon" for example, then that would be ideal
The main thing is that all the folder contents get moved to the appropriate folder- the script doesn't have to literally merge everything, although that would be helpful. Also it is critical that the script can look through multiple different folders and not just one sequence at a time.
Thanks for any help you can give!
Upvotes: 1
Views: 443
Reputation: 3792
This script will examine all files recursively, and for every file that matches the stated regex in the file name, it will move that file to a folder whose name matches the first capture group in parentheses in the regex search.
Update: I have updated the script to isolate regex matching.
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
Upvotes: 1
Reputation: 3792
This script will recursively move all files. It puts the burden on you to first provide the list of folder names you're gonna look for in the individual files.
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
Upvotes: 1