Write TXT File

Hi, there: A Question: Is it possible to write these data to a txt file (not a csv). I would use this data to modify a php file.

Is a sample available for the function node ?
Thanks,
Stefan

In what format? So how would the data of the columns be separated?

Like this:
$config[‘KIZEO’][‘USERID’][‘030’] = “206351”;
$config[‘KIZEO’][‘USERID’][‘031’] = “367300”;

Example:
$config[‘KIZEO’][‘USERID’][‘carid here’] = “driver here”;

Then i like to write it via write binary

You understand ?

I have a old application here that is configurated via php files.

You can do this:

{
  "nodes": [
    {
      "parameters": {
        "fileName": "/tmp/file.txt"
      },
      "name": "Write Binary File",
      "type": "n8n-nodes-base.writeBinaryFile",
      "typeVersion": 1,
      "position": [
        950,
        300
      ]
    },
    {
      "parameters": {
        "mode": "jsonToBinary",
        "convertAllData": false,
        "options": {
          "useRawData": true
        }
      },
      "name": "Move Binary Data",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        800,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      driver: 'name1',\n      carid: '001',\n    }\n  },\n  {\n    json: {\n      driver: 'name2',\n      carid: '002',\n    }\n  }\n]"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        400,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const fileData = items.map(item => `$config['KIZEO']['USERID']['${item.json.carid}'] = \"${item.json.driver}\";`)\n\nreturn [\n  {\n    json: {\n      data: fileData.join(\"\\n\"),\n    }\n  }\n]"
      },
      "name": "Convert Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Move Binary Data": {
      "main": [
        [
          {
            "node": "Write Binary File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mock Data": {
      "main": [
        [
          {
            "node": "Convert Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert Data": {
      "main": [
        [
          {
            "node": "Move Binary Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Jan, this works: i put this Codesnippet in my storage to use it in other projekts. Only one question:
How i can put in the first line a “<?php”

Thanks again,
Stefan

Great to hear!

It is possible like this:

{
  "nodes": [
    {
      "parameters": {
        "fileName": "/tmp/file.php"
      },
      "name": "Write Binary File",
      "type": "n8n-nodes-base.writeBinaryFile",
      "typeVersion": 1,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "mode": "jsonToBinary",
        "convertAllData": false,
        "options": {
          "useRawData": true
        }
      },
      "name": "Move Binary Data",
      "type": "n8n-nodes-base.moveBinaryData",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      driver: 'name1',\n      carid: '001',\n    }\n  },\n  {\n    json: {\n      driver: 'name2',\n      carid: '002',\n    }\n  }\n]"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const fileData = items.map(item => `$config['KIZEO']['USERID']['${item.json.carid}'] = \"${item.json.driver}\";`)\n\nreturn [\n  {\n    json: {\n      data: '<?php\\n' + fileData.join(\"\\n\"),\n    }\n  }\n]"
      },
      "name": "Convert Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        700,
        300
      ]
    }
  ],
  "connections": {
    "Move Binary Data": {
      "main": [
        [
          {
            "node": "Write Binary File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mock Data": {
      "main": [
        [
          {
            "node": "Convert Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert Data": {
      "main": [
        [
          {
            "node": "Move Binary Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Have fun!