Had some time prior to departure, so decided to post the AppleScript (easier to read) Automator solution. Test it on a sample directory of SKU filenames to measure result.
Steps:
- Launch /Applications/Automator.
- New Document
- Application
- Click Choose
- Drag and drop Library>Files & Folder>Ask for Finder Items into the larger right-hand workflow window.
- Start at: Desktop
- Type: Folders
- No Allow Multiple Selection
- Drag and drop Library>Automator>Run AppleScript, or Library>Utilities>Run AppleScript below the previous action in the workflow window. Whichever is present.
- Select and remove all content in this action's window.
- Copy and paste the code below into the Run AppleScript action's window
- Click the hammer icon to compile the action's content
- Save your Automator application to your Desktop with a catchy name like SKU2Folder. Double-click the application to be prompted for the parent folder to process the SKU filenames. When the whirling Automator activity in the right menu bar ends, you will know its done, unless I add more code.
- Quit Automator
Code:
# AppleScript to process filenames, create directories based on first numeric
# filename string prior to space in name, and then move those matching files
# into their namesake folder.
# Reference: https://discussions.apple.com/thread/252838354
# Tested: macOS 11.4
# VikingOSX, 2021-06-07, Apple Support Communities, No warranty/support expressed or implied.
use scripting additions
property DELIM : {space}
on run {input, parameters}
# get selected folder passed from the Ask for Finder Items Automator action
set sfolder to (item 1 of input) as alias
tell application "Finder"
# make a list of every regular file in the selected folder
set fList to (every item in folder sfolder whose kind is not "Folder") as alias list
repeat with anItem in fList
# get filename from the individual path
set anItemx to anItem's name
# get variable number string up to space in filename from list
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, DELIM}
# example: {"9357753500167", "1.jpg"}
set basename to item 1 of (text items of anItemx)
set AppleScript's text item delimiters to TID
# ignore the error if the folder already exists
try
make new folder at sfolder with properties {name:basename}
end try
move anItem to folder ((sfolder & basename) as text) with replacing
end repeat
end tell
return input
end run