You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automate moving files into folders with the same name

I have several images Per SKU that I'd like to move into their respective folders with the same name (see attachment) with a batch script as opposed to manually. As I have hundreds of images to move daily, I'd love to automate this process.


There can be anywhere from 1-5 images with titles ranging from 7 to 13 characters. For the below example I'm going with 13 chars – up until the space.


I'm terrible at understanding scripting so I was hoping someone out there could help.


Thank you in advance


Posted on Jun 6, 2021 2:53 AM

Reply
Question marked as Top-ranking reply

Posted on Jun 6, 2021 6:19 AM

I have just tested a short script that when given a starting folder, processes all files in that folder into their respective 7 or 13 character folder names. I have used five files per SKU in the test. I have some questions.


  1. Will there be files in sub-directories within the starting folder requiring the script to be recursive in that hierarchy?
  2. Are there files that use a different character (e.g. underscore) instead of the space character that you have shown in your example filenames? Different filenames altogether (e.g. alphanumeric, other characters, etc.)?
  3. Do you work in the Terminal, or prefer an Automator application that lets you choose the starting folder and does the rest for you?
8 replies
Question marked as Top-ranking reply

Jun 6, 2021 6:19 AM in response to benbowstyle

I have just tested a short script that when given a starting folder, processes all files in that folder into their respective 7 or 13 character folder names. I have used five files per SKU in the test. I have some questions.


  1. Will there be files in sub-directories within the starting folder requiring the script to be recursive in that hierarchy?
  2. Are there files that use a different character (e.g. underscore) instead of the space character that you have shown in your example filenames? Different filenames altogether (e.g. alphanumeric, other characters, etc.)?
  3. Do you work in the Terminal, or prefer an Automator application that lets you choose the starting folder and does the rest for you?

Jun 7, 2021 5:06 AM in response to VikingOSX

hey VikingOSX, thanks for getting back to me.


to answer your questions:


  1. there aren’t usually files in sub directories. It will usually go foldername, file 1, file 2, file 3 etc. in the one location.
  2. We usually have spaces and they are always numbers (SKU). At least for the part we’re searching for. Sometimes there a characters after the sku like CM. RT but I’ll usually remove that prior.
  3. I don't know how to use Terminal so choosing the starting folder is my preference at the moment


I hope that’s enough info for you

Jun 7, 2021 6:37 AM in response to benbowstyle

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:

  1. Launch /Applications/Automator.
    1. New Document
      1. Application
      2. Click Choose
    2. Drag and drop Library>Files & Folder>Ask for Finder Items into the larger right-hand workflow window.
      1. Start at: Desktop
      2. Type: Folders
      3. No Allow Multiple Selection
    3. Drag and drop Library>Automator>Run AppleScript, or Library>Utilities>Run AppleScript below the previous action in the workflow window. Whichever is present.
      1. Select and remove all content in this action's window.
      2. Copy and paste the code below into the Run AppleScript action's window
      3. Click the hammer icon to compile the action's content
    4. 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.
    5. 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


Automate moving files into folders with the same name

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.