Check if file exists

Hi all,
please is there any way to check if the file already exists? I tried to do it with readBinaryFile and IF but without results.

Thank you.

I think this will give you what you are looking for:

It uses the executeCommand node to run a /bin/sh command (if [ -r /etc/hosts ]; then echo Exists; fi) to check if the file /etc/hosts exists on the local system. It will spit out the word Exists if the file exists. The IF statement then checks for the presence of the word Exists and executes on the true connector if it does.

This will only work on a Linux host as the command in the execudeCommand node is OS specific. If you need to run this on a Windows host, you would use something like IF EXIST C:\WINDOWS\SYSTEM32\ETC\HOSTS Exists. (Not certain what it would be on a Mac but would be similar to if [[ -r /etc/hosts ]]; then echo Exists; fi as I’m assuming it is running /bin/bash.)

Hope that helps.

1 Like

Hi @Tephlon thank you for your advice. It seems to be working for second and other rounds but for the first round it’s quite odd. The following screenshot is for the first round (when the file does not exist):

In the first round (when the file does not exist) it goes to the “creating the file section” but also to another condition.

And this is for the second time - this looks fine (file already exists at this moment).

Do you have any tips? I can not see anything wrong.

Hey @stehos!

Did you create a condition for the false branch? By default, it will match on any result and run in all instances. I get the feeling that this is what is happening with your flow.

Let me know if I can be of further assistance.

Hey @stehos!

Everything looks good on the execute node (although I would rename it). What I was referring to was the IF1 node and I may have steered you in the wrong direction. :frowning_face:

Based on what you have here, it should only trigger the false route when /data/fe.json does not exist.

Could you post the JSON for IF1?

Hi @Tephlon I got it. I didn’t realize that IF has an output for true / false and it’s not working like IF ELSE statement. Now I added other IF to the false branch and it prevents other steps.

Thank you for your help! :slight_smile:

Hey @stehos,
Yeah, that is a bit of a “gotcha” for the IF statement. Glad that it is working now!