Cannot Use Dynamic Filenames To Write Files In Loops

by ADMIN 53 views

Bug Description

The issue at hand is related to the inability to write files dynamically using filenames in a loop. This problem arises when attempting to use dynamic filenames within a loop, resulting in an error. However, under normal conditions, it is possible to write files without any issues.

To Reproduce

To reproduce this issue, we need to examine two different scenarios. The first scenario represents a normal situation where files can be written without any problems. The second scenario, on the other hand, demonstrates the issue at hand where dynamic filenames cannot be used to write files in a loop.

Normal Situation

In the normal situation, we have a JSON file that contains a workflow with a "Read/Write Files from Disk" node. This node is connected to a "Set" node, which is responsible for setting the filename for the file to be written. The filename is set using a dynamic expression that includes the value of the "file_name_ch" variable.

{
  "nodes": [
    {
      "parameters": {
        "operation": "write",
        "fileName": "={{ $('Create HTML1').item.json.file_name_ch }}.png",
        "options": {}
      },
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1,
      "position": [
        800,
        640
      ],
      "id": "5d4ffe61-d375-4a15-881b-2306fc17488a",
      "name": "Read/Write Files from Disk1"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "ad325c1b-1597-45ab-98cd-1801da32e3f1",
              "name": "data",
              "type": "string",
              "value": "=1"
            },
            {
              "id": "4ec8161e-eb4c-4fd0-86d3-ba52a1271f26",
              "name": "file_name_ch",
              "value": "=S创上海2025 邀请函_{{ $json.name }}",
              "type": "string"
            },
            {
              "id": "f90fa355-b620-4146-aa1a-f4b3f2a053c7",
              "name": "file_name_en",
              "value": "=S-Tron Shanghai2025 Speaker Invitation_Sebastien_{{ $json.name }}.pdf",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "1d106ad2-7ea8-41b4-bbb9-cbb54f18eb36",
      "name": "Create HTML1",
      "type": "n8n-nodes-base.set",
      "position": [
        420,
        640
      ],
      "typeVersion": 3.3
    }
  ],
  "connections": {
    "Create HTML1": {
      "main": [
        [
          {
            "node": "Read/Write Files from Disk1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "343ca7113481b2697cee19da7d1b1fdc7bf625d8aab65bc6f8b9ba40f5c98118"
  }
}

Wrong Situation

In the wrong situation, we have a JSON file that contains a workflow with a "Split in Batches" node. This node is connected to a "Set" node, which is responsible for setting the filename for the file to be written. The filename is set using a dynamic expression that includes the value of the "file_name_ch" variable. However, when we try to write the file using the "Read/Write Files from Disk" node, we encounter an error.

{
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        260,
        480
      ],
      "id": "6157ad89-2669-4c47-9eff-8f3e681467cd",
      "name": "Loop Over Items"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "ad325c1b-1597-45ab-98cd-1801da32e3f1",
              "name": "data",
              "type": "string",
              "value": "="
            },
            {
              "id": "4ec8161e-eb4c-4fd0-86d3-ba52a1271f26",
              "name": "file_name_ch",
              "value": "=S创上海2025 邀请函_{{ $json.name }}",
              "type": "string"
            },
            {
              "id": "f90fa355-b620-4146-aa1a-f4b3f2a053c7",
              "name": "file_name_en",
              "value": "=S-Tron Shanghai2025 Speaker Invitation_Sebastien_{{ $json.name }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "cce17bb3-80d0-43e9-befb-a5a29571a2fe",
      "name": "Create HTML1",
      "type": "n8n-nodes-base.set",
      "position": [
        480,
        500
      ],
      "typeVersion": 3.3
    },
    {
      "parameters": {
        "operation": "write",
        "fileName": "={{ $('Create HTML1').item.json.file_name_ch }}.png",
        "options": {}
      },
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1,
      "position": [
        1020,
        500
      ],
      "id": "64a56afd-2909-4171-b842-68c9f73f84c8",
      "name": "Read/Write Files from Disk1"
    }
  ],
  "connections": {
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Create HTML1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create HTML1": {
      "main": [
        [
          {
            "node": "Read/Write Files from Disk1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read/Write Files from Disk1": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "343ca7113481b2697cee19da7d1b1fdc7bf625d8aab65bc6f8b9ba40f5c98118"
  }
}

Expected Behavior

The expected behavior is that we should be able to use dynamic filenames to generate files in a loop. This means that when we run the workflow, it should be able to write files with dynamic filenames without any issues.

Operating System

The operating system being used is Windows 11.

n8n Version

The version of n8n being used is 1.92.2.

Node.js Version

The version of Node.js being used is 22.

Database

The database being used is SQLite (default).

Execution Mode

The execution mode being used is main (default).

Conclusion

Q: What is the issue with using dynamic filenames to write files in loops?

A: The issue is that when using dynamic filenames within a loop, the workflow encounters an error and is unable to write files with the dynamic filenames.

Q: What is the expected behavior when using dynamic filenames to write files in loops?

A: The expected behavior is that the workflow should be able to write files with dynamic filenames without any issues.

Q: What are the system requirements for this issue?

A: The system requirements for this issue are:

  • Operating System: Windows 11
  • n8n Version: 1.92.2
  • Node.js Version: 22
  • Database: SQLite (default)
  • Execution Mode: main (default)

Q: What are the steps to reproduce this issue?

A: To reproduce this issue, follow these steps:

  1. Create a workflow with a "Split in Batches" node.
  2. Connect the "Split in Batches" node to a "Set" node.
  3. In the "Set" node, set the filename for the file to be written using a dynamic expression that includes the value of the "file_name_ch" variable.
  4. Connect the "Set" node to a "Read/Write Files from Disk" node.
  5. Run the workflow.

Q: What are the possible causes of this issue?

A: The possible causes of this issue are:

  • Incorrect configuration of the "Split in Batches" node.
  • Incorrect configuration of the "Set" node.
  • Incorrect configuration of the "Read/Write Files from Disk" node.
  • Incompatible versions of n8n and Node.js.

Q: How can I resolve this issue?

A: To resolve this issue, follow these steps:

  1. Check the configuration of the "Split in Batches" node, "Set" node, and "Read/Write Files from Disk" node to ensure that they are correct.
  2. Check the versions of n8n and Node.js to ensure that they are compatible.
  3. If the issue persists, try updating n8n and Node.js to the latest versions.

Q: What are the best practices for using dynamic filenames to write files in loops?

A: The best practices for using dynamic filenames to write files in loops are:

  • Use a consistent naming convention for dynamic filenames.
  • Use a unique identifier for each file to avoid overwriting existing files.
  • Use a try-catch block to handle any errors that may occur when writing files.
  • Use a logging mechanism to track any errors that may occur when writing files.

Q: Can I use dynamic filenames to write files in loops in other workflows?

A: Yes, you can use dynamic filenames to write files in loops in other workflows. However, you should ensure that the workflow is properly configured and that the versions of n8n and Node.js are compatible.