Working With Files and Folders

Node-red has 2 core nodes that allow you to read and write to files.

However if you want to explore the file system you will need to:

  • Install additional nodes
  • Use the exec node with OS commands
  • Use the nodejs fs module.





In this tutorial we will cover all of these possibilities starting by looking at the 2 core nodes.

Using the Read and Write File Nodes

read-write-files-nodes
These node are part of the core nodes and allow you to read in data from a file and write data to a file.

Data is stored as binary data, but the data can represent text,in which case and encoding method needs to be specified. By default utf8 encoding us used for text data.

When working with binary data we uses buffers and when working with text data we use strings.

The file name can be configured in the node but is generally passed into the node as msg.filename. The path is relative to the working directory which is generally .node-red.

The options are shown in the screen shot below:

filename-options

The configuration of these nodes is very similar:

The read node configuration is show below:

read-file-node-config

The write node configuration is show below:

write-file-node-config

An Interesting application of the write node is that it can be used to delete a file.

JSON data is text data and is written and read as text data.

Listing Files and Folders – node-red-contrib-fs

You will need to install the node-red-contrib-fs module through the manage Palette menu.

file-lister
The node appears as fs file lister under the storage group.

The configuration screen for the node is shown below and is covered in the video below:

 

file-lister-node-config

Listing Files and Folders-Using the Exec node.

The exec node allows you to run external commands and so we can use it to run standard operating system file commands.

On Linux systems we can use the ls command to list files and folder, the rm command to delete files etc.

On Windows systems we can use the dir command to list files and folder, the del command to delete files etc.

Using the node.js fs module

You can add this module to the function node or add it system wide by editing the settings.js file. See Using Node (npm) Modules in Node-Red.

As an example the fs module which gives access to the file system and lets you create folders,read and write files etc has sync and async versions of most functions.

As an example to create a directory you can use either the async call of

fs.mkdir()

of the sync call of:

fs.mkdirSync()

You may be thinking that the async call is always better, but if you have in your function node that does something like this:

1.create new folder
2.write a file to the new folder

You may find that you are trying to write a file to a folder that doesn’t yet exist if you use the async version.

Using this module is very useful if you have existing node.js code that you can reuse in node-red.

Node-Red Additional Nodes

In addition there are several nodes that you can install that will allow you to list files and folders.

If these nodes aren’t enough then you can always use the external fs module in the function node and also use the operating system file commands via the exec node.

Demo flow

You can download a demo flow that illustrates the above using the link below:

file-nodes

Related tutorials and resources

 

Click to rate this post!
[Total: 0 Average: 0]

Leave a Reply

Your email address will not be published. Required fields are marked *