# Reading and Writing into a text file from the function node

**URL:** <https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073>\
**Category:** General\
**Created:** [21 January 2019 03:45 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073 "2019-01-21T03:45:01Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![KevinPamu](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@KevinPamu](https://discourse.nodered.org/u/KevinPamu)\
**Post date:** [22 January 2019 15:02 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073/23 "2019-01-22T15:02:25Z")

</div>

Sorry, I should have made it more clear. I have a query that writes in to a file (say file A) using the file in node(using the /n). The first query returns around 20-30 messages. So in file A I have the 20 messages plus an extra 21st line that's empty. I have another function that takes one line at a time from file A and pumps it into the query and returns some more information for me. So After the last line from file A is read, the empty 21st line is also read from the file. which I am trying to avoid.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 January 2019 15:05 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073/24 "2019-01-22T15:05:10Z")

</div>

> [@KevinPamu](#):
>
> I have a query that writes in to a file (say file A) using the file in node(using the /n)

I am afraid you still have not answered the question. I presume you mean a File Out node, but the question is when you write to the file are you writing the complete file with one message or sending the file out node multiple messages, one for each line in the file?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 January 2019 15:06 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073/25 "2019-01-22T15:06:12Z")

</div>

Sorry I messed up the last message, I have editted it.

---

<div class="post-metadata">

**Author:** ![KevinPamu](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@KevinPamu](https://discourse.nodered.org/u/KevinPamu)\
**Post date:** [22 January 2019 15:07 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073/26 "2019-01-22T15:07:39Z")

</div>

Sorry again. using the file out node I write one line at a time, not the entire content of the file.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 January 2019 15:09 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073/27 "2019-01-22T15:09:38Z")

</div>

> [@KevinPamu](#):
>
> I write one line at a time, not the entire content of the file

In that case put a debug node monitoring the data going to the file. Possibly you are sending an extra empty message, so the file includes a blank line on the end, or possibly the last line you send already has a newline on the end so it ends up as two lines when the node adds the extra one.  
[Edit] I presume you have looked at the file with a text editor or similar to confirm that there really is a blank line on the end, and that it is not your flow when you read it in again that is adding the extra query.

---

<div class="post-metadata">

**Author:** ![KevinPamu](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@KevinPamu](https://discourse.nodered.org/u/KevinPamu)\
**Post date:** [22 January 2019 16:47 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073/28 "2019-01-22T16:47:27Z")

</div>

@Colin it was because the last message is always adding a new line to the file. I solved it by using a split node and sending the line to the query only if it is not empty.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 January 2019 16:50 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073/29 "2019-01-22T16:50:54Z")

</div>

> [@KevinPamu](#):
>
> it was because the last message is always adding a new line to the file.

The last message will not add a new line to the file unless there is something different about it. It does not add a new line for previous messages so how does the file out node know that the last one is the last one unless it is different to previous ones?

---

<div class="post-metadata">

**Author:** ![KevinPamu](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@KevinPamu](https://discourse.nodered.org/u/KevinPamu)\
**Post date:** [22 January 2019 18:11 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073/30 "2019-01-22T18:11:50Z")

</div>

@Colin when I said "it was because the last message is always adding a new line to the file" that was the database values from SQL. wasn't a problem with the file node. It was just easier for me to use a split node.

PS: I should probably work on my communication, sorry I confused you a few times.

---

<div class="post-metadata">

**Author:** ![KevinPamu](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@KevinPamu](https://discourse.nodered.org/u/KevinPamu)\
**Post date:** [22 January 2019 20:39 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073/31 "2019-01-22T20:39:29Z")

</div>

Hi @Colin @dceejay @zenofmud @TotallyInformation @ukmoose, I am almost at the end of the project right now. Thank you again for helping me figure this out. One last question; In case I needed to restrict the file-out node to send just one message (just first line) to a function node and stop sending anymore lines, is there a node I can use in between the file-out and the function node that will help me achieve that?

thank you.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 January 2019 21:02 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073/32 "2019-01-22T21:02:35Z")

</div>

How will whatever you put downstream to pass on only the first line know whether subsequent lines are the remainder of the file or the start of the file for the next time you read it?

---

<div class="post-metadata">

**Author:** ![KevinPamu](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@KevinPamu](https://discourse.nodered.org/u/KevinPamu)\
**Post date:** [22 January 2019 21:22 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073/33 "2019-01-22T21:22:32Z")

</div>

It will be overwritten the next time the file is read. I just need the first line.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 January 2019 21:33 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073/34 "2019-01-22T21:33:33Z")

</div>

I think you don't understand my point. If you configure the file node to send a message for each line then the downstream logic must pass on the first message then ignore the following messages. But for how long must it continue to ignore messages? It would have to have some way of when to expect a new 'first' line again.  
I think the easiest way will be to configure the file node to read the complete file and pass it on as one string. Then you can have a function node that just passes on the first line and throws the rest away.  
Another alternative would be to use an Exec node to execute a command that just reads the first line of a file. The command

```auto
head -1 filename

```

would do that. That assumes you are running in a Linux environment of course.

---

<div class="post-metadata">

**Author:** ![KevinPamu](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@KevinPamu](https://discourse.nodered.org/u/KevinPamu)\
**Post date:** [23 January 2019 04:29 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073/35 "2019-01-23T04:29:07Z")

</div>

any other ideas? I tried the suggestion Colin gave me, will work on it tomorrow.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 January 2019 09:39 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073/36 "2019-01-23T09:39:08Z")

</div>

> [@KevinPamu](#):
>
> I tried the suggestion Colin gave me

Do you mean it did not work?

---

<div class="post-metadata">

**Author:** ![KevinPamu](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@KevinPamu](https://discourse.nodered.org/u/KevinPamu)\
**Post date:** [23 January 2019 19:55 UTC](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073/37 "2019-01-23T19:55:59Z")

</div>

Thank Colin, I had to work with windows environment but I used the same idea, it worked.

[Previous page](https://discourse.nodered.org/t/reading-and-writing-into-a-text-file-from-the-function-node/7073.md?page=1)
