Search for text file

Hi Everyone,
I am having a problem trying to do a quick check to see if a file exists. In theory it should be quite easy but for some reason and after spending several hours trying to get it to work I am admitting defeat and posting here for help! I have searched this forum and also ChatGPT for help but am still not able to find my text file! I just need to check if a text file exists in a folder and if it does great, just carry on down, if it does not then inform me via message so I can add the file and continue down the pipeline. The print screen should be self explanatory as it's quite straight forward (in theory) but I am also attaching the offending macro. Note that the Job report file will have different names depending on the job so I cannot just search for Job Report.txt and therefore have to search for .txt instead.



Text file check.kmmacros (4.3 KB)

I'm still reading it, but I can see one small error that COULD cause your problem. You have "The Variables" at the top of your macro. Remove that. Also, based on your problem description, I think you also remove "The Finder's Selection".

P.S. I think this problem can be solved without a loop at all. Not that it's "wrong" to use a loop, if you want to, but if a single IF statement can solve it, why not be simple?

I think this problem can be solved without a loop at all. Not that it's "wrong" to use a loop, if you want to, but if a single IF statement can solve it, why not be simple?

Quite honestly because i am not great a keyboard maestro and am learning, so yeah totally open to a new / better way of doing it as it's highly likely that I have just totally overcomplicated the whole thing!

I'm working on it. I'm no wizard here, I have to work to get good answers.

Here's one way that seems to be working. There are probably dozens of alternate solutions. My way may not be the best way, and I hope someone improves on it.

Of course, you have to replace my path with your path. And then you can replace my Display actions with whatever code you want to insert.

image

1 Like

Thank you very much for your help. Just cannot get this to work for some reason! I would have thought that this would be quite a simple thing to do but for some reason its not working, whichever way I try to solve it...
Screenshot 2025-03-22 at 10.48.46

Did you enclose the Execute Shell Script action inside the "Try" action as my macro specifies? I don't think you did. Are you willing to upload your macro so I can verify that you copied my code?

Also, based on your error message, it seems you didn't include "*.txt" as my code indicated. Are you willing to upload your macro so that I can verify this?

You can use an If action with the Path condition to determine whether something exists at that path or not.

Read about it in the KM wiki here condition:Path [Keyboard Maestro Wiki]

In this case, I don't think that works, because the Path condition doesn't accept wildcards. He specifically requires a wildcard check.

Ignore this, it was 100% wrong :).

-rob.

You missed his opening paragraph:

Note that the Job report file will have different names depending on the job so I cannot just search for Job Report.txt and therefore have to search for .txt instead.

Ah, so I did—thanks!

-rob.

It looks like the Filter action set to Path Extension might get the file info you need.

Here's your macro with this added action. Oh, I took out the Finder's Selection collection for testing purposes.

Screenshot

Text file check-extension check.kmmacros (4.7 KB)

1 Like

You have a space in your path ("Booking Files") but you haven't escaped it or quoted the full path in your shell script. Try

ls /Users/DarrenMorton/Desktop/Booking\ Files/New/*.txt

or

ls "/Users/DarrenMorton/Desktop/Booking Files/New/*.txt"

I would probably use:

  • Filter Text with Standardize Path (to expand any ~, etc), and
  • a path condition (If > At this Path > something exists)

e.g.:

File exists in folder ?.kmmacros (5.9 KB)

Note that there are other path condition options:

Screenshot 2025-03-23 at 11.50.21 am