Write a line feed to a file

Okay, another question, that I am assuming should be easy and I’m just missing. So, I’m pulling data from a website, I split out the results, sort, then convert to csv including header row using convert to file node, then send that to Write file to disk.

All that works great. But, if I have another page of data indicated, I loop, parse the cursor, pass it to a new web get, get the results, do the same split and sort, but this next time, I convert to csv without the header, and write to disk using append.

Now, the issue I’m running into is that there is no carriage return and line feed on the last line of the write to disk. So when I do the first append, the first line is at the end of the last line.

So, my question is, can I write a carriage return and line feed to the file using the write to file, or would I have to use a code node and write js?

Information on your n8n setup

  • n8n version: 1.95.3
  • Database (default: SQLite): Default
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): npm
  • Operating system: Ubuntu 24.04

I think injecting a new line for the first iteration of writing to file is the easiest way out for you, something like this (inside the Code node right before you Write to File) may do the trick, see if it helps:

const item = $input.all()[0];

const fileName = item.binary.data.fileName;
const mimeType = item.binary.data.mimeType;

const originalBuffer = await this.helpers.getBinaryDataBuffer(
  0, 'data'
);

const updatedBuffer = Buffer.concat(
  [originalBuffer, Buffer.from('\n', 'utf8')]
);

const updatedBinary = await this.helpers.prepareBinaryData(
  updatedBuffer, fileName, mimeType
);

return [
  {
    binary: {
      ['data']: updatedBinary
    }
  }
];

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