Move All Files in Selected SubFolders to Parent Folder [Example]

Hey JM (@JMichaelTX),

This is one of those places where I dislike using Keyboard Maestro native actions, because it's not intuitive and no fun at all to create the necessary steps.

Find a Folder in a Directory By Its Suffix v1.00.kmmacros (7.8 KB)

I don't particularly like using vanilla AppleScript for this either, because in a folder with many files/folders whose clauses can be pretty slow to glacial .

If I know in advance the source folder will have less than 100 or so items I may resort to a whose clause, but it's still relatively slow on my older hardware with macOS 10.12.6 (Sierra).

This AppleScript takes 22 seconds to run on a folder with 100 items in it (on my system).

tell application "Finder"
   set sourceFolder to target of front window as alias
   set itemList to (items of sourceFolder whose name ends with "sufx") as alias list
end tell

By contrast this shell script is very quick and efficient:

sourceDir=~/Desktop
folderSuffixStr='*sufx'

find "$sourceDir" -depth 1 -type d -iname "$folderSuffixStr"

We can also do VERY quick with AppleScriptObjC:

------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/05/08 07:01
# dMod: 2019/06/16 17:25
# Appl: AppleScriptObjC
# Task: Non-Recursively Find Files Using a Regular Expression ; RegEx ; Handler ; Insertion-Location.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Finder, @Find, @Files, @RegEx
------------------------------------------------------------
use framework "Foundation"
use scripting additions
------------------------------------------------------------

# Front Finder Window (or Desktop if no windows are open).
tell application "Finder" to set sourceFolder to insertion location as alias

set sourceFolder to POSIX path of sourceFolder
set folderSuffix to "sufx"

set foundItemList to its findFilesWithRegEx:("(?i)^.+" & folderSuffix & "$") inDir:sourceFolder

------------------------------------------------------------
--ยป HANDLERS
------------------------------------------------------------
on findFilesWithRegEx:findPattern inDir:sourceFolder
   set fileManager to current application's NSFileManager's defaultManager()
   set sourceURL to current application's |NSURL|'s fileURLWithPath:sourceFolder
   set theURLs to fileManager's contentsOfDirectoryAtURL:sourceURL includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
   set theURLs to theURLs's allObjects()
   set foundItemList to current application's NSPredicate's predicateWithFormat_("lastPathComponent matches %@", findPattern)
   set foundItemList to theURLs's filteredArrayUsingPredicate:foundItemList
   set foundItemList to (foundItemList's valueForKey:"path") as list
end findFilesWithRegEx:inDir:
------------------------------------------------------------

-Chris