creating recurring named folders with Automator

Hello,

I want to use Automator to create multiple folders with the name based on the name of the first file.

I have a list of files like this:

ABT_001.jpg

ABT_002.jpg

ABT_003.jpg

ABT_004.jpg

ABT_005.jpg

ABU_001.jpg

ABU_002.jpg

ABU_003.jpg

ABU_004.jpg

ABU_005.jpg

ABV_001.jpg

ABV_002.jpg

ABV_003.jpg

ABV_004.jpg

ABV_005.jpg

ABX_001.jpg

ABX_002.jpg

ABX_003.jpg

ABX_004.jpg

ABX_005.jpg


I would like to place the ABT files in a folder named ABT the ABU files in a folder names ABU the ABV files in a folder named ABV.

Of course I have the option in finder to make a new folder with selection and rename the folder BUT when you have hundreds of folders to make this can be pretty boring.

So I tried to use Automator but it looks like it is unable to make a folder based on a PORTION of the name of the files you choose to be in it.


Anyone can help me with this?


Thank you in advance.

iMac (27-inch, Late 2013), macOS Mojave (10.14)

Posted on Oct 16, 2018 7:47 AM

Reply
Question marked as Top-ranking reply

Posted on Oct 16, 2018 6:14 PM

Here is a Ruby script (scrollable) that can replace the boilerplate in the standard Automator Run Shell Script action. If you have two actions in an Automator application workflow:

  • Finder library : Ask for Finder Items
    • Select single parent folder for the enclosing folder of the files you want to organize
  • Automator library : Run Shell Script
    • Shell: /usr/bin/ruby
    • Pass in: As Arguments
    • Remove the default boilerplate Bash for loop in the Run Shell Script action and replace it with the following Ruby code
  • #!/usr/bin/ruby """ pf.rb - Using the first three characters of the filenames make new folders with these strings, and then move every file with matching first three characters into its corresponding folder. Usage: ./pf.rb parent folder passed in from command-line or Automator Reference: https://discussions.apple.com/thread/8586956 Tested: macOS 10.14.0 with System Ruby version Version: 1.2 Author: VikingOSX, 2018-10-16, Apple Support Communities, No warranties of any kind. """ require 'fileutils' require 'set' newfolders = Set.new allfiles = Set.new these_files = [] regex = /\A([A-Z]{3})(?=_)/ afolder = File.expand_path(ARGV.first) # get an ordered set of new folder names from the filenames Dir.glob("#{afolder}/*.{jpg,jpeg,jp2}").sort.each do |f| newfolders << regex.match(File.basename(f)).captures.join allfiles << f end # make the new folders, and move matching files into them newfolders.each do |f| FileUtils.mkdir(File.join(afolder, f)) these_files = allfiles.grep(/#{f}/) these_files.map do |x| FileUtils.mv(x, File.join(afolder, f.to_s, File.basename(x))) end end


    I tested this as a standalone Terminal script, and as an Automator application. I used your list of filenames as the test files, and the matching files were moved into the correct folders.

    Similar questions

    4 replies
    Question marked as Top-ranking reply

    Oct 16, 2018 6:14 PM in response to sgflaviu

    Here is a Ruby script (scrollable) that can replace the boilerplate in the standard Automator Run Shell Script action. If you have two actions in an Automator application workflow:

  • Finder library : Ask for Finder Items
    • Select single parent folder for the enclosing folder of the files you want to organize
  • Automator library : Run Shell Script
    • Shell: /usr/bin/ruby
    • Pass in: As Arguments
    • Remove the default boilerplate Bash for loop in the Run Shell Script action and replace it with the following Ruby code
  • #!/usr/bin/ruby """ pf.rb - Using the first three characters of the filenames make new folders with these strings, and then move every file with matching first three characters into its corresponding folder. Usage: ./pf.rb parent folder passed in from command-line or Automator Reference: https://discussions.apple.com/thread/8586956 Tested: macOS 10.14.0 with System Ruby version Version: 1.2 Author: VikingOSX, 2018-10-16, Apple Support Communities, No warranties of any kind. """ require 'fileutils' require 'set' newfolders = Set.new allfiles = Set.new these_files = [] regex = /\A([A-Z]{3})(?=_)/ afolder = File.expand_path(ARGV.first) # get an ordered set of new folder names from the filenames Dir.glob("#{afolder}/*.{jpg,jpeg,jp2}").sort.each do |f| newfolders << regex.match(File.basename(f)).captures.join allfiles << f end # make the new folders, and move matching files into them newfolders.each do |f| FileUtils.mkdir(File.join(afolder, f)) these_files = allfiles.grep(/#{f}/) these_files.map do |x| FileUtils.mv(x, File.join(afolder, f.to_s, File.basename(x))) end end


    I tested this as a standalone Terminal script, and as an Automator application. I used your list of filenames as the test files, and the matching files were moved into the correct folders.

    This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

    creating recurring named folders with Automator

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