Compare folders count = execute macro

This seems so obvious but cannot seem to find the answer. Here is the process: I am creating postscript files of a folder that I've selected with the mouse. These postscript files go to an Adobe Distiller's "watched" folder. The postscripts are processed into PDFs and sent to the "watched" folder's "OUT" folder. What I need help with is...I would like to get the file count of both folders (the one I selected initially and the "OUT" folder). When they both have the same number of files, the rest of the macro needs to continue. Any ideas. This was really easy in QuicKeys. Thanks.

It is easy in KM also. The main KM Action you need is:
Execute a Shell Script action

like this:

image

Where "Local__FolderA" is the full path to the folder of interest.
The hard part here is knowing which Bash commands to use. In this case, it is:

  • ls -- List files (one per line) in $folder
  • wc -- Count the number of lines

So, all you need is to execute that Action for each folder, and then do a compare using If Then Else action.

So here's the complete example macro to get you started. You will, of course, need to adapt it for your specific use.

Please let us know if this works for you. Feel free to post any questions, comments, issues, and/or suggestions concerning this macro. If you have another problem/issue/question, please post in a new topic.


MACRO:   Compare Count of Files in Folders [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/b/3/b3d6f8073e0ed62ef9b5702a318a57cbe836d537.kmmacros">Compare Count of Files in Folders [Example].kmmacros</a> (9.1 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

### ReleaseNotes

HOW TO USE
	1.	Enter the folder path into each of the magenta Actions below
	2.	Trigger this macro

Author:  JMichaelTX

**NOTICE: This macro/script is just an _Example_**

* It is provided only for _educational purposes_, and may not be suitable for any specific purpose.
* It has had very limited testing.
* You need to test further before using in a production environment.
* It does not have extensive error checking/handling.
* It may not be complete.  It is provided as an example to show you one approach to solving a problem.

**REQUIRES:**

1. **KM 8.2+**
  * But it can be written in KM 7.3.1+
  * It is KM8 specific just because some of the Actions have changed to make things simpler, but equivalent Actions are available in KM 7.3.1.
.
2. **macOS 10.11.6 (El Capitan)**
  * KM 8 Requires Yosemite or later, so this macro will probably run on Yosemite, but I make no guarantees.  :wink:

---

![image|560x1613](upload://sqxdAzt6Pmkbbk5EEhoAwud8WN2.jpg)

---

### Shell Script
```bash
#!/bin/bash
read folder

'ls' -1 "$folder" | wc -l
```

@wolfixum, one followup to this. You may need to use the Bash find command instead of the ls command. See:

Thanks Michael! I was experimenting with this previously but only one of the two folders is in a set location all the time (if you read my process description you will see). The other folder are folders that are randomly put around my computer depending on the job so I need to "select" the folder by clicking on the folder. This works well for other scripts by using "The Finder's Selection." I was hoping there was a way to get the count of a folder that was selected.

Thanks for your help!
Steve

OK. I'm not a UNIX genius by any stretch of the imagination. I've executed your script replacing the folder paths with direct paths and no matter how many files are in a folder they are always equal which tells me that the script isn't really seeing either of the folders and they are at zero. I've tested out the script starting at "ls" within terminal and it's returning the correct number of files in a folder. From what I can tell the script looks clean. Since I'm not a UNIX guy (yet) and I've read the link you've supplied about BASH (which I will need to read many times over to try to understand) could it be there is a small typo within your script or am I missing something completely? Thanks!

Hi Steve,

Here's another way of going about this that only uses KM actions. I don't know if this is faster than the shell script method (though it's probably not) but it should still be fast enough and will hopefully be easier to tweak/understand. Make sure to set the path to the OUT folder in the bottom For Each action, where it says [SET FOLDER PATH] before running the macro.

Compare Folder File Counts.kmmacros (6.3 KB)
image

Hello gglick.
This is easier to understand and seems to work only the first time I run it. Once I take some files out of a folder and run, it appears to not zero out the results run last time so it's always true with the same numbers that were show before. But the big thing is that I can now select a folder and get a file count which was what I was stuck on. Hopefully I can figure out how to tweak your script to zero out everything before it runs because the two Setting Variables at the beginning apparently do not zero everything. If anyone has any insight into this issue, I would love to hear about it. Thank you so much! It has helped me move a little further. Cheers!
~Steve

Any idea why gglcik's script has this alert? It's probably why it only worked one time for me. Sorry, I'm still trying to understand the KM language.

Solved! The issue was user error. I just realized I had several Watched folders I never knew existed. I pointed to the wrong one. Everything works perfectly. Thanks for all the help and all the info. I learned a lot!

Cheers,
Steve

1 Like

You don't need to replace anything in the Execute Shell Script Action.
Just set the folder paths for "A" and "B" (in your case that would be your selection folder and "OUT" folder), and run the script.

I just tested the macro and, and it works fine, using these folder paths:

image

IAC, I have made a major revision to the Macro which should make it easier for you to use and understand. To test this macro, please make ONLY one change to the Macro. Set the Variable _ Local__OutFolder_ to the path of the "watched" folder's "OUT" folder.

image

Here's the revised macro. Please test and let us know if it works for you.


Example Output

image

image


MACRO:   Compare Count of Files in Folders [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/e/d/ed5e065d173f063b4a4e1a47d738943f4f390960.kmmacros">Compare Count of Files in Folders [Example].kmmacros</a> (22 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

### ReleaseNotes

HOW TO USE
	1.	Enter the OUT folder path into the magenta Action below
	2.	Trigger this macro

Author:  JMichaelTX

**NOTICE: This macro/script is just an _Example_**

* It is provided only for _educational purposes_, and may not be suitable for any specific purpose.
* It has had very limited testing.
* You need to test further before using in a production environment.
* It does not have extensive error checking/handling.
* It may not be complete.  It is provided as an example to show you one approach to solving a problem.

**REQUIRES:**

1. **KM 8.2+**
  * But it can be written in KM 7.3.1+
  * It is KM8 specific just because some of the Actions have changed to make things simpler, but equivalent Actions are available in KM 7.3.1.
.
2. **macOS 10.11.6 (El Capitan)**
  * KM 8 Requires Yosemite or later, so this macro will probably run on Yosemite, but I make no guarantees.  :wink:

---

![image|458x1399](upload://9V2RbxOvgqeFzdQME3jR9McqDzl.jpg)

---

### Shell Script to Count Files in Folder

```bash
#!/bin/bash
# Use when you need to use StdIn from the KM Action in a command that does not accept StdIn

myFolder=$(cat)

find "$myFolder"  -maxdepth 1 -type f -not -path '*/\.*' | wc -l
```

Questions, issues, comments, suggestions?

Thanks so much Michael. It does work well, unfortunately I prefer to select the source folder with my mouse before I run the script which is what the other script does. I'm sorry you put so much time into this but the other script really does exactly what I want. I'm going to study this script to learn more about scripting outside of KM with unix scripting and I'm sure there will be times when I will need this script in the future. Again, thank you all for your quick responses. An amazing group!
Steve

No problem. Use the solution that works best for you.
Of course, you can always make your own solution built from parts of others.
If you wanted to use my macro starting with the source folder you have selected in the Finder, it just requires one KM Action:

image

IAC, good luck!

Thanks!!!

Hey Steve,

QuicKeys had a lot of very nice Finder actions incorporating AppleScript under-the-hood.

Gabe and JM have given you some useful solutions, so I won't bother to create a turnkey macro – but here are the basic AppleScript building blocks for your task:

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/07/08 19:52
# dMod: 2018/07/08 19:52 
# Appl: Finder, System Events
# Task: Get Finder-Selection and Count Files in Two Folders
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @System_Events, @Selection, @Count, @Files, @Folder
----------------------------------------------------------------

# Get the single selected folder in the Finder.
tell application "Finder"
   set finderSelectionList to selection as alias list
   if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
   set selectedFolder to POSIX path of item 1 of finderSelectionList
end tell

set outFolder to "~/Downloads/Out/" -- POSIX-Path or Tilde-based-POSIX-Path

# Count files in each of the designated folders.
tell application "System Events"
   set outFolderContentsCount to count of files of disk item outFolder
   set selectedFolderFileCount to count of files of disk item selectedFolder
end tell

# Set Keyboard Maestro variables from AppleScript.
tell application "Keyboard Maestro Engine"
   setvariable "outFolderContentsCount" to outFolderContentsCount
   setvariable "selectedFolderFileCount" to selectedFolderFileCount
end tell

----------------------------------------------------------------

-Chris

Thanks Chris!