Move Same Prefix Files to a New Folder Named With That Prefix

Hi,
I'm new to KM and I don't know how to create folders based on a Prefix (ex: S010A02) and move all the files containing that prefix into a new folder named with the Prefix.
In the screenshot there's an exemple with the first folder manually created.
Many thanks!!!!
Screenshot_2021-01-18 22.05.05_kM4toD|690x1066

Moderator Edit: In the future, please just paste, drag-n-drop, or insert via the forum menu all of your images so they appear in the post.

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.
Note that I have NOT done any error checking, which you might want to add.
One good thing is that if the "new" subfolder already exists, it will NOT error -- it will just use that folder.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Create Sub-Folder and Move All Files with Given Prefix [Example]

-~~~ VER: 1.0    2021-01-18 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Create Sub-Folder and Move All Files with Given Prefix [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


Example Output

Files BEFORE Macro is Run

Files AFTER Macro is Run

image


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Create Sub-Folder and Move All Files with Given Prefix

HOW TO USE

  1. First, make sure you have followed instructions in the Macro Setup below.
  2. Trigger this macro.
  3. Choose a File with the Prefix that you want to Move.
    • Process assumes that the Prefix ends with an Underscore ("_")

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. ??

Make These Changes to this Macro

  1. Assign a Trigger to this macro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    (all shown in the magenta color)
    • TBD
      • DESC

REQUIRES:

  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.11.6 (El Capitan)+

TAGS: @Files @Move @Example

==USE AT YOUR OWN RISK==

  • While I have given this a modest amount of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

1 Like

Many thanks @JMichaelTX, well appreciated.
What I actually need is to create, for each new suffix, a folder.
The trigger would create S010A01 and place all the files containing the prefix S01A01 into it (as the macro you sent me), but also for S010A02, S010A03, S010A02, etc.
Is it possible?

Of course it is possible, but that is NOT what you asked for:

It is always best to present your complete set of requirements in your initial post.
Please read:
Tip: How Do I Get The Best Answer in the Shortest Time?

Sorry, I don't have any more time right now. You can just run the Macro for each prefix you need. Further modifications will be left as an exercise for the student. :wink: This is the best way to learn.

Try making the mods yourself. If you really get stuck, post back with your Macro and specific questions, and maybe someone can help.

Got it, thanks for your help!

Dear @JMichaelTX, I spent 10 hours trying to build the macro, with no success :sweat_smile:

Need help please!

As I mentioned above, if you need help please post your best Macro along with specific questions. See How to upload your macro.

Hi,
Here's exactly what I want, it will really save me tons of time:

Here's the macro I'm trying to create during hours but with no success:
New Folder.kmactions (383 B)

Thanks in advance for your help!

OK @Omar_Tajmouati, here's the revised Macro that will create a sub-folder, and move the file, for ALL files that have a prefix that ends with an underscore _.

Note that this macro can be further optimized to run a bit faster, but it seems to run very fast to me.

This will attempt to create the Sub-Folder even if it already exists.
Does NOT cause an error
Although redundant, seems very fast even if Sub-Folder already exists.

BTW, this is an excellent use case for RegEx, used to extract the Prefix from the file path:
\/([^\/]+?)_
For details see regex101: build, test, and debug regex

Before Macro is Run

image

AFTER Macro is Run

image

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

==UPDATED==: 2021-01-22 16:12 GMT-6

MACRO:   Create Sub-Folders and Move All Files for All Prefixes [Example]

-~~~ VER: 2.1    2021-01-22 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Create Sub-Folders and Move All Files for All Prefixes [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Create Sub-Folders and Move All Files with Given Prefix

HOW TO USE

  1. First, make sure you have followed instructions in the Macro Setup below.
  2. Trigger this macro.
  3. Choose the Source Parent Folder that contains the files you want to Move.
    • Process assumes that the Prefix ends with an Underscore ("_")

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. ??

Make These Changes to this Macro

  1. Assign a Trigger to this macro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.

REQUIRES:

  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.11.6 (El Capitan)+

TAGS: @Files @Move @Example

==USE AT YOUR OWN RISK==

  • While I have given this a modest amount of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

Hi, many thanks for your prompt response!

It didn't work for me, here's what I get:

OK, no problem. I did not allow for other underscores in the file name.
Here's the fix:

In this Action:
image

Change the RegEx to:
\/([^\/]+?)_

I'll update the above macro.

Worked like a charm!!!!
Many thanks!!!

1 Like