How Do I Get Count of Files in Folder when Folder Trigger is Invoked?

to easily count how many files are in that folder. And I'm sure it can easily do that. But I haven't been able to find anything in the User Guide that tells how to do it... Can anybody help? Thanks!

You can use the For Each Path in Folder action found in the Control Flow and File categories. This allows you to process each file (and folder) in the trigger folder without specifically needing to know how many there are.

But if you do want an actual number:

As you can see, there are 10 files (and folders) in this particular trigger folder.

You can also do it with AppleScript:

tell application "System Events" to get the number of items in folder "~/Example" whose visible is true

The term items refers to files and folders, and you can exchange this for the term files or the term folders to get a specific count for each (not that the For Each... action mentioned above returns the count for both files and folders (collectively referred to as files or paths, so just mind the fuzzy terminology).

You can also do it with shell script:

ls ~/Example | wc -l
1 Like

Excellent! Thanks!

There is actually a separate topic where that question has been asked/answered, with a number of different solutions to choose from:

Thanks!

-jg

Only the small secrets need to be protected. The big ones are kept secret by public incredulity.
--Marshall McLuhan

Hey,

I know this is an old post, but according to what i understand the example above works with when the folder is set in advance. Is there a way you can ask the user which folder to work with, then count that folder?

Certainly:

Folder File Count.kmmacros (2.3 KB)
image

In addition to the excellent macro that @gglick provided, here is a macro which provides several means for specifying the Folder Path, and may be faster if there are many (>> 100) files in the folder:

MACRO: Get Count of Files in Folder [Example]

It makes use of a fast, reusable Bash Shell Script:
image

1 Like

Excellent! Thanks guys. If one were to filter the result to a specific file type, how would you go about it? (Sorry, total noob at KM, but love it so far)

In the type of macro I demonstrated, this can be accomplished by enclosing the action used to count the number of files with an If Then Else action. Using PDF files as an example:

image

Easy enough. Change two actions in my Macro:

Add FileExt Variable to Prompt

image

Add name Filter to find Command

-name "*.$KMVAR_Local__FileExt"

image

The Bash find command is very powerful, and very compact to use.
For more info, see Bash Find Command Document

Questions?

thanks :slight_smile: