Skip to content

Files Manipulation

File Manipulation

  • READ_FILE <file_path> TO <variable_name>: Read a file's contents.
  • WRITE_FILE <file_path> WITH <variable_name>: Write to a file.
  • DELETE_FILE <file_path>: Remove a specific file.
  • CHECK_FILE_EXISTS <file_path> TO <variable_name>: Check if a file exists.

Example - File Manipulation

Example 1: Read from a file and publish its content.

READ_FILE /path/to/file.txt INTO fileContent
PUBLISH /fileContent WITH fileContent

Explanation
  1. Read File Content The content of the file located at /path/to/file.txt is read and stored into the fileContent variable.

    READ_FILE /path/to/file.txt INTO fileContent
    
  2. Publish File Content The fileContent variable is then published to the /fileContent MQTT topic.

    PUBLISH /fileContent WITH fileContent
    

    For example, if the content of the file is "Hello, World!", this message will be published to the /fileContent MQTT topic.

Example 2: Write to a file.

SET message TO "Hello, World!"
WRITE_FILE /path/to/file.txt WITH message

Explanation
  1. Set Message The message variable is set to "Hello, World!".

    SET message TO "Hello, World!"
    
  2. Write to File The message variable is then written to the file located at /path/to/file.txt.

    WRITE_FILE /path/to/file.txt WITH message
    

    This could be used, for example, to store data into a file for later

File Manipulation FAQ

What types of files can I manipulate with Flux?

Flux allows you to manipulate text files, configuration files, and other small data files. It is not designed for manipulating large files or binary files.

Can I read and write to the same file multiple times?

Yes, you can read from and write to the same file multiple times using the READ_FILE and WRITE_FILE commands.

What happens if I try to delete a file that does not exist?

The DELETE_FILE command will return an error if the file does not exist. This can be captured and handled using error events in Flux.

Is it possible to check if a file exists before performing operations on it?

Yes, you can use the CHECK_FILE_EXISTS command to check if a file exists before performing operations on it.