# Read txt file in folder flle by file

**URL:** <https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904>\
**Category:** General\
**Created:** [19 August 2021 09:20 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904 "2021-08-19T09:20:28Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![chawana](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@chawana](https://discourse.nodered.org/u/chawana)\
**Post date:** [19 August 2021 09:20 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904/1 "2021-08-19T09:20:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)\
**Post date:** [19 August 2021 09:26 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904/2 "2021-08-19T09:26:21Z")

</div>

We are using [node-red-contrib-fs-ops](https://flows.nodered.org/node/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?

---

<div class="post-metadata">

**Author:** ![chawana](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@chawana](https://discourse.nodered.org/u/chawana)\
**Post date:** [19 August 2021 09:35 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904/3 "2021-08-19T09:35:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)\
**Post date:** [19 August 2021 09:40 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904/4 "2021-08-19T09:40:21Z")

</div>

Then you just need to loop through this list (array?) and use the ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/2/724a602a94986335612347377cd5ea39bfce3705.png) node for example to read the content.

---

<div class="post-metadata">

**Author:** ![chawana](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@chawana](https://discourse.nodered.org/u/chawana)\
**Post date:** [19 August 2021 09:45 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904/5 "2021-08-19T09:45:02Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/55f5e9481586220720f9083e3c97b57badc092b8.png)  
not sure how to config this node

---

<div class="post-metadata">

**Author:** ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)\
**Post date:** [19 August 2021 09:47 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904/6 "2021-08-19T09:47:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chawana](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@chawana](https://discourse.nodered.org/u/chawana)\
**Post date:** [19 August 2021 10:10 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904/7 "2021-08-19T10:10:14Z")

</div>

what is action seection?

---

<div class="post-metadata">

**Author:** ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)\
**Post date:** [19 August 2021 10:17 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904/8 "2021-08-19T10:17:43Z")

</div>

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:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/8/88761c9b6ab03e02d2d370245648f2cb38e3c808.png)

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [19 August 2021 16:19 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904/9 "2021-08-19T16:19:44Z")

</div>

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 `join`ed (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.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [19 August 2021 16:25 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904/10 "2021-08-19T16:25:04Z")

</div>

> [@rko](#):
>
> I must admit, the names of these nodes can be confusing

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.

https://trello.com/c/9QTo2ELg.html

---

<div class="post-metadata">

**Author:** ![rko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rko/32/45807_2.png) [@rko](https://discourse.nodered.org/u/rko)\
**Post date:** [19 August 2021 19:28 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904/11 "2021-08-19T19:28:43Z")

</div>

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 😉

Maybe a note could be added to the “[General guidance](https://nodered.org/docs/creating-nodes/)” section to define the terms. Something like:

**A flow is the centre of the ~~universe~~ galaxy ☀**  
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)

---

<div class="post-metadata">

**Author:** ![chawana](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@chawana](https://discourse.nodered.org/u/chawana)\
**Post date:** [21 August 2021 15:41 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904/12 "2021-08-21T15:41:19Z")

</div>

Do you have exsample flow for guilddance?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 August 2021 15:54 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904/13 "2021-08-21T15:54:14Z")

</div>

> [@chawana](#):
>
> Do you have exsample flow

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

> [@shrickus](#):
>
> `list files` -\> 2. `split` -\> 3. `change` -\> 4. `file in` -\> 5. `join` -\> 6. `process array`

Here is something to get you started...

![Oyp4P11IBx](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f6c47806e1adb94dc706774d530d98399fa47655.gif)

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![chawana](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@chawana](https://discourse.nodered.org/u/chawana)\
**Post date:** [21 August 2021 16:10 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904/14 "2021-08-21T16:10:01Z")

</div>

Thanks Steve.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 October 2021 16:10 UTC](https://discourse.nodered.org/t/read-txt-file-in-folder-flle-by-file/49904/15 "2021-10-20T16:10:41Z")

</div>

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