Create Folder with variable names

I have a root path in a variable, and a folder name I want to create in that path in another variable.

When I do the action:

Create New Folder In Destination
%Variable%rootPath%/%Variable%thisFolderName%

(ie “path/folder”)

I would expect my new variable-named folder to be created at my path.

What I actually get is a folder called “%Variable%thisFolderName%” at the correct path.

So it seems the variable expansion is only working for the first variable.
Should I be concatenating the path name differently?

Thanks!

KM 8.2

FolderCreate.kmmacros (1.6 KB)

I have mimicked what I gather your post is asking about using KM 8.2.

The macro runs successfully.

%Variable%rootPath%/%Variable%thisFolderName%

was simply copied from your submission.

If I run this macro, then a folder called testFolder appears on the Desktop.

The macro that I posted is actually strangely forgiving.

The following variants also work (note the presence or absence of the “/” at end of rootPath or in the name of the Create New Folder

rootPath = /Users/owl/Desktop/
Create New Folder “%Variable%rootPath%%Variable%thisFolderName%”

rootPath = /Users/owl/Desktop
Create New Folder “%Variable%rootPath%/%Variable%thisFolderName%”

rootPath = /Users/owl/Desktop/
Create New Folder “%Variable%rootPath%/%Variable%thisFolderName%”

It seems to “tolerate” having what is essentially two “/” or one specified in either location.

Thanks for the response - very strange it's working for you - you're not doing anything different what I did, and expected to work!

I see the double forward slash in your first example - I don't have a trailing slash on my first path variable, but even so, it should work (especially as you say how "forgiving" it is for you!)

I must go and hack it about some more...

Hey @beely,

When posting question of this sort please also post a working test case macro (insofar as is possible).

People are much less likely to help you if they have to recreate your macro from scratch to test.

-Chris

Thanks, will do.

Hey @beely,

I this working macro I'm making CERTAIN I create a correct file path.

You should not be concerned here with what Keyboard Maestro or the macOS will tolerate. You should be concerned with how to construct a correct file path on Unix.

Create New Folder.kmmacros (5.9 KB)

As you can see I've disabled the Create New Folder action in favor of a Display Text in Window action.

This lets me completely visualize the file path I've built, and I can test it in the Terminal or a BBEdit shell worksheet (my general preference for this sort of thing).

Here is the created path:

/Users/chris/test_directory/KM_TEST/Test Folder

It will NOT work in a normal shell environment, because of the embedded spaces. These need to be quoted or escaped.

Quote example 01:

'/Users/chris/test_directory/KM_TEST/Test Folder'

Quote example 02:

/Users/chris/test_directory/KM_TEST/'Test Folder'

Escaped example 01:

/Users/chris/test_directory/KM_TEST/Test\ Folder

These methods MUST be used IF you're working in a shell environment.

BUT — Keyboard Maestro understands path strings with spaces in them and will escape them for you.

The big issue for you in this task is to make certain you've properly divided the nodes in your path with a forward slash.

To see if a path string works you can create your file or folder in the location you want it and then use C in the Finder to copy the item's path string.

If that matches the path string you've created in your macro then you're good to go.

-Chris

Thanks all - I just got back to having another look at this.

I put an Alert displaying the exact same concatenation string as in the Create Folder action, and the path/folder variables were expanded and displayed correctly.

I then re-enabled the Create Folder action, and it’s creating the destination folder correctly now, with no changes to the macro steps.

So I was doing it correctly, it just seems that KM had got into a weird state where it wasn’t expanding the variable correctly. Sounds like a bug to me - if it happens again, or I can lock down a set of steps to reproduce, I’ll post back.

Thanks again.

The new folder macro doesn't work for me in 10.13.6, same error as rlivingston's solution above

2019-11-07 20:40:43 New folder /Users/me/Downloads/test_directory/KM_TEST/Test Folder failed with error Error Domain=NSCocoaErrorDomain Code=4 "The file “Test Folder” doesn’t exist." UserInfo={NSFilePath=/Users/me/Downloads/test_directory/KM_TEST/Test Folder, NSUnderlyingError=0x604000659230 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}} In macro “Create New Folder” (while executing Create New Folder “%Variable%local_RootPath%/%Variable%local_FolderName%”).

I had to run a shell script instead

    cd ~/_baseFolder
    mkdir $KMVAR_local_FolderName