Read txt file in folder flle by file

Someone have sample code/ node read text file file by file for guildline for me?

We are using node-red-contrib-fs-ops (fs-ops-dir) to list files from a folder where Node-RED has access to.

What have you tried so far?

I can list files name is done but can't read file in floder with file by file until nothing file

Then you just need to loop through this list (array?) and use the image node for example to read the content.

not sure how to config this node

I think this one is for writing files. Try the "file in" node and use msg.filename as input (the path to the file).

what is action seection?

I think you are still using the wrong node. The file in node (for reading files) does not have an action section, only the file node (for writing files). Through the action parameter you can define what should be done if a file exists by the time you are trying to write to it. For example, if you would like to add lines to an existing file with content, you would use the append action.

I must admit, the names of these nodes can be confusing. However, if you hover with your mouse over a node in the left sidebar you can see a short description of the node:

In node-red, "looping" over a list is handled by a pair of split/join nodes, with the processing placed between those nodes -- conceptually, it would look like this:

  1. list files -> 2. split -> 3. change -> 4. file in -> 5. join -> 6. process array

Some file node or flow (1) is used to get a list of filenames
The list is split (2) into separate msg objects, each being sent downstream.
You can use a change (3) node to set msg.filename from the incoming payload.
Then the file in (4) node uses that filename field to read in the file contents.
The stream of file content msgs are joined (5) into a single array, in the original order.
Optionally, you can use a function (6) to process the array of text blobs into an output file.

Funny how something so obvious (the fact those names are confusing) can be missed when you stare at it for so long. We can do better. Have added an item in the backlog to set the paletteLabel property of those nodes to be read/write.


Read/Write sounds good. I think another problem with the terms in and out is that they’re not being used consistently. However, it is easy to guess the meaning if a node has only one connector :wink:

Maybe a note could be added to the “General guidance” section to define the terms. Something like:

A flow is the centre of the universe galaxy :sunny:
If you are using the terms in and out, your point of view should be the flow where the node is added. In means, something is going or coming into the flow (for example file in: data from the file system is coming into the flow). Out means, something is leaving the flow (for example serial out: send a command to the serial port).

I am sure a native speaker can do a better job in explaining the conventions. The only problem I see with this convention is, that an in-node has an output and an out-node has an input.

For example, node-red-contrib-socketcan is doing the opposite compared to the file or serial node:

  • socketcan-in (has an input): send commands to the CAN-Bus
  • socketcan-out (has an output): read CAN frames (into the flow)

Do you have exsample flow for guilddance?

Did you try to follow what @shrickus suggested...

Here is something to get you started...


[{"id":"f99bdc54af6749b2","type":"fs-ops-dir","z":"62a0e5a5.fc59bc","name":"get directory listing","path":"path","pathType":"msg","filter":"*.txt","filterType":"str","dir":"payload","dirType":"msg","x":1950,"y":100,"wires":[["1442125127e1aa55"]]},{"id":"58dd07eaa76a732b","type":"inject","z":"62a0e5a5.fc59bc","name":"msg.path = c:/temp","props":[{"p":"path","v":"c:/temp","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1740,"y":100,"wires":[["f99bdc54af6749b2"]]},{"id":"3b669a4473627687","type":"function","z":"62a0e5a5.fc59bc","name":"create full path","func":"msg.file = msg.payload;\nmsg.filename = msg.path + \"/\" + msg.file;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1730,"y":180,"wires":[["359e2cf5c02ee91e"]]},{"id":"1442125127e1aa55","type":"split","z":"62a0e5a5.fc59bc","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":2140,"y":100,"wires":[["3b669a4473627687"]]},{"id":"e68a4b5b7f86dc27","type":"file in","z":"62a0e5a5.fc59bc","name":"","filename":"","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":1720,"y":260,"wires":[["6042eb738695b957"]]},{"id":"6042eb738695b957","type":"function","z":"62a0e5a5.fc59bc","name":"do something with file data","func":"msg.payload = \"SMAC: \" + msg.payload.slice(0,24)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1950,"y":260,"wires":[["e30b490d307ef288","663275511f9bcfef"]]},{"id":"663275511f9bcfef","type":"debug","z":"62a0e5a5.fc59bc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":2220,"y":260,"wires":[]},{"id":"359e2cf5c02ee91e","type":"delay","z":"62a0e5a5.fc59bc","name":"delay for viewing purposes","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"2","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"x":1970,"y":180,"wires":[["e68a4b5b7f86dc27"]]},{"id":"e30b490d307ef288","type":"join","z":"62a0e5a5.fc59bc","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":1880,"y":340,"wires":[["dce3a64c73439c54"]]},{"id":"dce3a64c73439c54","type":"debug","z":"62a0e5a5.fc59bc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":2220,"y":340,"wires":[]}]
1 Like

Thanks Steve.

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