Finding DOCX files inside a multiple-level folder hierarchy

Oh man... I need to learn my shell scripts! They are awesome!!! Am I right in thinking you can dump either of those to a text file using >> notation?

find ~/Documents/MAIN -name "*.docx" >> ~/myDocs.txt

1 Like

Yes you can use >> to append to a file.

Or if you want to process the files in a macro, you can put the output in a variable and then to For Each line in the variable.

1 Like

Thanks. Is it possible to use a KM variable (for the path name) in the FIND (or MDFIND) command with the KM Shell Command? It doesn’t seem to work for me.

Hey Steve,

Of course.

You didn't post your code.

See this:

https://wiki.keyboardmaestro.com/action/Execute_a_Shell_Script

Look for “Keyboard Maestro variable”

-Chris

Here’s the code that’s not working - thanks:

Hey Steve,

So you're trying to use a text-token in a shell script.

The text token would be %Variable%Path_Stub% NOT %Path_Stub%, but that is not the correct syntax for a shell script.

KMVAR_YourVariableName is correct.

So:

mdfind -onlyin "$KMVAR_Path_Stub"

* Use quotes to make certain spaces are correctly treated in path strings in the shell.

Keyboard Maestro understands $HOME-based paths (e.g. ~/Downloads), but you can run into issues when a path contains spaces and you pass a home-path to the shell.

You can't quote the home part of the path – you must:

~/Downloads/'my folder name with spaces'/

Or you can escape the spaces of course.

The simpler way to handle this in Keyboard Maestro is:

-Chris

1 Like

(deleted comment)

Thanks Chris - it works!

Yvan Koenig has used Shane's code to make a very user-friendly handler that should work for you.
###See on getItemsIn:posixPathOfFolder

3 Likes

That seems to work as well… Thanks.

Shane's ASObjC script should be much faster than the other approaches, especially if you have lots of files in the folder/subfolders. Let me know if you don't see this.

1 Like

Is it possible to do something like this in KM? (If so, it may avoid some Regex solutions?)

Actually ASObjC should not be faster than Spotlight. Spotlight should eat ASObjC's lunch, since it's an indexed search.

Test show them to be relatively comparable on my system though. (Bad Spotlight...)

Since the ASObjC search is not dependent upon indexing it will be the more bombproof of the two.

-Chris

2 Likes

Hey Steve,

This will work, but then how are you going to ID your files?

You'll have to use a regular expression or literal-text compare test.

It's also going to crawl the file hierarchy, so it will be much slower than mdfind or ASObjC.

-Chris

1 Like

Chris, maybe Spotlight is slower because it has to be run through a shell script?

1 Like

Yes. Use %Variable%Folder% as the path of the second For Each action.

Whether it solves your issue or not is a separate question.

1 Like

I’m having problems as I test this solution.

Just to be clear (in my For Each x 2 image) –

Will my FOLDER var get just FOLDERS?
Will my FILE var just get FILES?
Does KM (and these vars) understand the difference between files and folders?

Thanks.

Steve, I'm confused on your objective.

The above solutions provide you with a list of file paths in the main folder and all sub-folders. The For Each Action will allow you to process each file in that list. With that approach you do not need to worry about the sub-folders -- they are included in the path of each file.

If this doesn't clear things up for you, then perhaps you can provide us with more details of exactly what you want to do.

I’m asking these questions for use in other situations. Thanks.

Please start a new topic when the subject changes.
Otherwise, it is confusing for all, particularly for future readers.