How do I find just the DOCX files inside a multiple-level folder hierarchy?
I can't predict how many subfolders (or levels of subfolders) will be inside the first level folders.
There will be 1 or more DOCX files somewhere inside each of the top level folders - but could be at any level in that folder's hierarchy.
Furthermore I then need to select just the 1 DOCX file in each top-level folder's hierarchy that has the latest "modified date". That's the part that seems to make this complicated.
In my example/graphic my top level folders are: "aaa" and "bbb".
See this post by Shane Stanley at MacScripter.net, which provides a great, fast ASObjC script:
Get File Path List in all subfolders
His current script does NOT provide for a filter like file type, but I have asked Shane if he can provide one.
Using ASObjC in AppleScript will be much, much faster than using calls to the Finder.
mdfind -onlyin ~/Downloads 'kMDItemFSName == "*.docx"c'
Run from an Execute a Shell Script action.
NOTE: The command is dependent upon Spotlight.
Shane’s AppleScriptObjC solution will ultimately be more bombproof, but this method is usually good enough.
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
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.
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.
You didn’t post your code.
Look for “Keyboard Maestro variable”
Here’s the code that’s not working - thanks:
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.
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:
Yvan Koenig has used Shane’s code to make a very user-friendly handler that should work for you.
###See on getItemsIn:posixPathOfFolder
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.
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.
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
Chris, maybe Spotlight is slower because it has to be run through a shell script?
Yes. Use %Variable%Folder% as the path of the second For Each action.
Whether it solves your issue or not is a separate question.