Simple way to check a file size ? (watch file?)

Hi !

I download a snap shot of a camera that sometime does not succeed
This can be easily checked from filesize (ie: filesize very small = wrong file) and I wanted to use this to filter wrong files for further processing.

I tried to use the "watch" node, but I'm not sure why, sometimes it will not send the "msg.size"

to be clear, sometime it will output:

payload: "/tmp/snap5.jpg"
topic: "/tmp/snap5.jpg"
file: "snap5.jpg"
filename: "/tmp/snap5.jpg"
size: 0
type: "file"

and sometime:

payload: "/tmp/snap6.jpg"
topic: "/tmp/snap6.jpg"
file: "snap6.jpg"
filename: "/tmp/snap6.jpg"
type: "none"

msg.type is different and size is here or not, no idea why.
I do not delete file, just overwrite with a different file to check what it detects.

Is this expected behavior of watch node ?
Is there an other way, like a simple function command I could use to read a file size ?

I'm not certain of the issue but I do know that file system watches are not always going to do what you think because of the way that systems have different ways of updating files.

Really, you need some delays in a watch - rather like a debounce function when listening to physical switches - you can get some transient noise in a switch and some transient changes to a file.

The reason this happens is that the watch node triggers as soon as it sees the new file, which will often be before the file write has completed. In fact, as @TotallyInformation says, you may (usually will) get multiple events from the watch node for each file. The easiest solution is to use a Trigger node set to Send Nothing, then wait for a time then Send the Latest Message.

Well, I waited a few minutes to check, but it looks like I receive only one message (with missing size & type='none').
And the point is that I do not wish to wait for too long: my goal is to trigger a snapshot & send the valid ones "asap"

I think I may have found a workaround with node-red-contrib-fs : it can provide in a single msg all stats about files matching a defined pattern.

I see you are storing it in /tmp which ( I think) may be in ram rather than a real disc. I wonder whether that is a factor.

I don't think it is, but not 100% sure (I run nodered in a docker, and it's /tmp inside the docker).

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.