Getting all "Level-1" File- & Folder-Names of a folder-path

Hello guys & girls (are there? :wink:) :smiley:
I tried to figure it out now for 1 hour, but I don't see a easy way.
Is it possible to get all "Level-1" File- & Folder-Names of a folder-path in ONE variable?

Example:
Ordner 1 contains:
dog.pdf
cat-folder
giraffe.flv

MACRO runs →

Variable-Content:
dog
cat
giraffe

(best would be a Conent-list WITHOUT file-suffixes fo my purpose)

I mean, there is a "Get File Name to Variable"-Action AND a "For Each Item in a Collection Execute Actions"-Action... but somehow I see no way, even, if I thnik, it must be easy.
I would be thankful for any suggestions.

Let's say Ordner 1 is the name of a sub-folder in your ~/Pictures folder. Then:

for f in ~/"Pictures/Ordner 1"/*; do
	printf '%s\n' "$(basename "${f%.*}")"
done

which, of course, can be saved to a variable instead of displayed in a window. The output returned is one filename per line, without filename extensions.

The important syntactical feature to note when copying and pasting the shell code above is the position of the double quotes where I've specified the folder path. ~/ is shorthand for your home directory (i.e. /Users/%you%/), and needs to remain outside the double quotes, with no space; and /* is shorthand for every file and folder at that path, and also needs to remain outside the double quotes, with no space.

3 Likes

And here's an all-native KM version as an alternative:

Get All "Level-1" File and Folder Names.kmmacros (3.1 KB)
image

The "base name" attribute in the Get File Attribute action returns a file's name without the extension, making this fairly easy to accomplish once you know about it.

8 Likes

@gglick
Amazing! Thank you so much! Exactly what I needed!

1 Like

@CJK Wow! Works great! Thank you! Good Shell Alternative!

Hey @John,

In your original post you show the above as a folder name.

Is “cat-folder” the literal name?

Or were you just indicating that “cat” was a folder?

-Chris

@ccstone
haha, great! I was just indicating that “cat” is a folder :slight_smile:

Hey @John,

Ah, I wanted to be sure before offering another alternative.

Here's one more way to do it with the shell:

find ~/'test_directory/KM_TEST/test_unicode' -mindepth 1 \( ! -name ".*" \) -exec basename {} \; |
sed -E 's![.].+$!!'

-Chris

1 Like

Exactly what I expected. Thank you!

Looking to modify so rather than declaring the file path within 'The files in the directory' collection, I use a 'Prompt for File' action to select a folder I want the contents listed out.

I see that I can use a 'Finder's Selection' Collection within the 'For Each' action and I also see that there is a 'Variables Collection' collection.

If I use the 'Prompt for File' action set to 'Prompt for Folder' and have the selected folder create a variable called 'Local__FolderPathForList', can I then use that variable in the next 'For Each' action? If so, how, and please explain.

Here's what I got:

Keyboard Maestro Actions.kmactions (2.5 KB)

Keyboard Maestro Export

The For Each Action can be exactly as @gglick's original but instead of a declared path, use the Variable you have obtained through the Prompt for Folder.

Get All "Level-1" File and Folder Names - Prompt for Folder.kmmacros (3.7 KB)

Click to Show Image of Macro

.

Ah, I see the 'The items in directory' option shows up in the 'Folder Contents Collection' and using the format %Variable%(Insert your Variable Here)% is how to place and use a variable that you've set. Thank you!

That basic level of instruction on how to use a variable isn't explicitly explained anywhere, is it, or did I miss it?