Filter Folders that do not contain specific words and rename / copy the reaming file

Hi Everyone,

I have been searching all over the internet, forums, etc. to work out how to filter out folders by their name and rename / copy the remaining folder. In my example (that is most likely not even close to correct!) the user is asked to input a File Name and then the contents of the folder named Booking Files is filtered to remove folders that are named 360, Blue Skies, New, Watermark. The remaining folder (there will only ever be one) is then named with the File Name (that is declared as a variable) before being copied to the desktop.

I am attaching a screenshot of my Macros and would like to see how this type of thing is done without scripts so I can learn. Thank you in advance for any help :slightly_smiling_face:



Prompt for User Input.kmmacros (4.9 KB)

You might want to change the if condition to does not match and use the following string: 360|Blue Skies|New|Watermark.

You could also use the copy action instead of move if you wanted.

1 Like

Thank you very much for your help :slightly_smiling_face:

1 Like

Glad to help, don’t hesitate to reach out if you need more info!

In case you are still working on this, here is a slightly amended version of what you were doing.

Moving folders containing files around using Keyboard Maestro is potentially a bit risky as there is no undo...

So, the below has a couple of safety checks added. First it checks if the file is actually a folder rather than a file (by checking the file type is "Directory"). And secondly (even though you said there would only ever be one odd folder) it stops after moving and renaming one folder (by using a Break from Loop Action).

Also, instead of using Matches 360|Blue Skies|New|Watermark (which would also match a folder called "360 test" or "360 Blue Skies" I used a list of "is not" tests. This can no doubt be done in Regex but that is beyond my Regex skill.

Move and Rename a Folder v2.00.kmmacros (6.2 KB)

Click to Show Image of Macro

1 Like

Hi @Zabobon and @Margate. The If action could be changed as follows. (Some might argue, however, that this version is more difficult to interpret.)

Keyboard Maestro Export

Aside: By convention, I suggest entering an Intentionally Blank Comment action. Otherwise when coming back to a macro, one might wonder if the else case was left undone.

1 Like

Hi @_jims that's great. I searched and couldn't find how to make the Regex only match exact matches in a multiple selection. So, the reason this works is that ^ says the start has to match and $ says the end of the phrase has to match, meaning in effect the whole phrase between each |?

2 Likes

You got it! ^ and $ are line anchors.

^ means that the following character or string must be at the beginning of the line
$ means that the preceding character or string must be at the beginning of the line.

They’re very useful, especially if you’re iterating over a bunch of lines that have a similar format.

2 Likes

Correct, @Zabobon.

But I was incorrect,

^360|Blue Skies|New|Watermark$

is NOT equivalent to:

^360$|^Blue Skies$|^New$|^Watermark$

Instead,

does not match ^360|Blue Skies|New|Watermark$

would be interpreted as:

local__name does not

start with 360 or
contain Blue Skies or
contain New or
end with Watermark

I corrected my post above.


Also, I should have mentioned that matches is case-sensitive by default. To make it case-insensitive, one would need to use:

(?i)^360$|^Blue Skies$|^New$|^Watermark$

To reinforce that it's actually case-insensitive, I'd probably write it as:

(?i)^360$|^blue skies$|^new$|^watermark$

...And if that wasn’t complicated enough... I would write it this way:

(?i)^(?:360|blue skies|new|watermark)$ :wink:

1 Like

Chris, good point. I like that better (and have probably done it in a similar way):

(?i)^(360|blue skies|new|watermark)$

I typically exclude the ?: prefix unless:

a. I'm using the Search and Replace action or the Search using Regular Expression action, AND

b. I want to define the group as non-capturing.

1 Like

I exclude where I can, but my RegEx is typically rather complex and uses a variety of capture groups... so as a default I go to non-capturing groups just to help visualize things better.

1 Like

One man's visual cue is another man's confusing syntax. And for me, it probably depends on the day of the week and the phase of the moon. :grinning:

Seems like a good place for this xkcd. :wink:

1 Like