Count of files inside a folder

I am trying to get the count of files inside a folder for later use in a macro. The problem is the for each loop. I found an old entry by @PeterLewis from 2020:

"You can count the number of files in a folder with:

  • Set variable “Count” to calculation “0”
  • For Each “Path” in Folder Contents collection
    • Set variable “Count” to calculation “Count+1”"

When I select the "for each path" action it comes back with the "for each item" action. I can select the folder contents collection, but do not see a path. I am also unsure what variable to set as the for each item. Does it matter if I only want the count? The error I get is that KM fails to evaluate the +1 calculation.

What am I missing here?

Set Variable to Calculation Action (v11.0.3)

Set Variable to Calculation.kmactions (465 B)

I would do it the easy way instead of the hard way:

image

That will give you the number of files in the folder that you specify in the action (~/data)

ls -1 ~/data | wc -l

P.S. It looks like you accidentally uploaded a single action instead of the whole macro. The instructions are on this page:

https://wiki.keyboardmaestro.com/Forum#How_to_PostUpload_Your_Macro_to_the_Forum

2 Likes

Just out of interest... I don't think the -1 flag is needed for ls there, is it?

man ls: "Force output to be one entry per line. This is the default when output is not to a terminal."

I believe you.

Oops, knew that. Just was a little quick. But I will try the shell script without the -1!

It gives me 0. With or without the -1. Just to be sure: I replaced the ~/data with the path to the folder to check. So, edited: ls ~/Users/.../Target PDF | wc -l

The paths are implicit, not explicit. That is, this is all you need:

Files in folder.kmmacros (4.1 KB)

What you set the "for each" variable to is, in this case, irrelevant, because you're not using it. And the path is what's returned in the "The items in directory" definition. You can test that by adding a Display Text action in the loop that displays the local_thePath variable (using my example). You'd see one window for every file in the folder, each window showing the full path to that file.

It would be helpful if you could show the command as you have it in the macro, but I suspect a problem with the space in your actual path. You need to enclose the reference in quotes, or backslash the space:

ls ~/Users/.../Target\ PDF | wc -l or ls "~/Users/.../Target PDF" | wc -l

-rob.

That path does not look right…

~ means the user's home directory, and so for user account "johnsmith" it is the same as: /Users/johnsmith.

~/Users would mean a folder called "Users" inside your home folder, and I doubt that that is what you mean.

If you select a folder in the Finder, you can drag it into KM (or the Terminal, or whatever...) and the path will be shown as text. That is convenient and a good way to avoid typos.

Actually, it was a copy, I just edited it a bit for obvious reasons. there were no spaces there.
But thank you!

I think you are right, but IMO it's prudent to include the -1 to explicitly indicate the intent. This is akin to using descriptive variable names in code: not absolutely required, but helpful for lifecycle support.

Another approach, for what it's worth:

Count of files-folders at path (if valid).kmmacros (3.5 KB)


return ObjC.deepUnwrap(
    $.NSFileManager.defaultManager
    .contentsOfDirectoryAtPathError(
         $(kmvar.local_FilePath)
         .stringByStandardizingPath,
         null
    )
)
.length
1 Like