Write File

What is the Write File Node in Node-RED?

The "Write File" node in Node-RED is used to write data to a file on the filesystem. It's commonly employed in flows where you need to save data or logs for later analysis or storage. In FlowFuse Cloud, the Write File node interacts with a cloud-based storage solution, leveraging AWS S3 for file storage. However, in the Node-RED instance running on edge devices using the FlowFuse device agent, this node will interact with the local file system of that device. The content to be written is specified using msg.payload.

Configuring the Write File Node in Node-RED:

  • Filename: Specify the filename

    • Path: Specify the path to the file where data will be saved.
    • msg: Use a message property to dynamically set the filename, By default, it will use msg.filename, and if msg.filename is used, the file will be closed after every write. For the best performance, use a fixed filename.
    • Expression: Use a JSON expression to set the filename dynamically based on data in the flow.
    • Environment Variable (env var): Use an environment variable to set the filename dynamically.
  • Action: Choose the action

    • Append to File: Adds new data to the existing file.
    • Overwrite File: Replaces the content of the file with new data.
    • Delete File: Removes the specified file from the filesystem.
  • Add Newline (\n) to Each Payload: Enabling this option will append a newline character to each payload before writing to the file.

  • Create Directory if it Doesn't Exist: Enabling this option will create the specified directory if it is not already present in the filesystem.

  • Encoding: Specifies the character encoding to be used when writing data to the file, when selecting "set by msg.encoding" you can set it dynamically.

Tip: Always use an absolute path for the filename to ensure Node-RED can accurately locate and manipulate the specified file.

Output

After the completion of the write operation, the input message passed to the write file node is sent to the output port.

Use Cases

  1. Data Logging: Store sensor readings, IoT device data, or system metrics into a file for historical analysis or monitoring trends over time.

  2. Error Logging: Capture and log error messages, exceptions, or debugging information to a file for troubleshooting and debugging purposes.

  3. User Inputs: Save user inputs or form submissions to a file, such as user preferences, feedback, or user-generated content.

  4. Reporting: Generate reports in CSV, JSON, or plain text formats and save them to a file for later retrieval or distribution.

  5. Backup and Recovery: Create backup files of critical data or system states for disaster recovery or version control purposes.

  6. Integration with External Systems: Save data retrieved from APIs, databases, or external services to a file for further processing or analysis.

  7. Archiving: Archive log files, historical data, or outdated documents to maintain a record of past events or changes.

  8. Data Transformation: Perform data transformation operations and write the transformed data to a file in a different format or structure.

  9. Event Logging: Log events, notifications, or user interactions to a file for auditing, compliance, or historical tracking purposes.

Examples

  1. In the example flow below, we demonstrate how to create a file and write content to it.
  1. In the example flow below, we demonstrate how to delete a file using the Write File node.

Node Documentation

Writes msg.payload to a file, either adding to the end or replacing the existing content. Alternatively, it can delete the file.

Inputs

filename string
The name of the file to be updated can be provided in the node configuration, or as a message property. By default it will use msg.filename but this can be customised in the node.
encoding string
If encoding is configured to be set by msg, then this optional property can set the encoding.

Output

On completion of write, input message is sent to output port.

Details

Each message payload will be added to the end of the file, optionally appending a newline (\n) character between each one.

If msg.filename is used the file will be closed after every write. For best performance use a fixed filename.

It can be configured to overwrite the entire file rather than append. For example, when writing binary data to a file, such as an image, this option should be used and the option to append a newline should be disabled.

Encoding of data written to a file can be specified from list of encodings.

Alternatively, this node can be configured to delete the file.