ESP32 Arduino SPIFFS: testing if file exists

In this tutorial we will learn how to check if a file exists in the ESP32 SPIFFS file system, using the name of the file we want to test. The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.

Introduction

In this tutorial we will learn how to check if a file exists in the ESP32 SPIFFS file system, using the name of the file we want to test. For an introductory tutorial on how to get started with the SPIFFS file system, please check here.

The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.

The Code

We will start our code by including the “SPIFFS.h” library. This will make available an extern variable called SPIFFS that we will use to interact with the SPIFFS file system of the ESP32.

#include "SPIFFS.h"

We will write the rest if the code in the Arduino setup function. As we commonly do, we will start by opening a serial connection in order to be able to output some messages from our program.

Serial.begin(115200);

Next we will need to mount the file system, before we can start interacting with it. To do it, we need to call the begin method on the already mentioned SPIFFS variable.

This method receives as parameter an optional Boolean value that specifies if the file system should be formatted automatically in case the mounting procedure fails. This optional parameter defaults to false but we will pass the value true to ensure it will be formatted in case something fails. Otherwise, we will not be able to interact with the file system.

As output, the begin method returns a Boolean value indicating if the mounting procedure was successful (true) or failed (false). We will apply an error check to the output of the method to make sure the file system mounted properly before proceeding.

if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
}

After that, we will write a file called “/test.txt”, so we can later check if it exists, which should return true. If you already have a file in the file system that you want to test, you can skip the creation part and use the name of that file.

Please check this previous tutorial for a detailed explanation on how to write a file. In short, we need to call the open method on the SPIFFS object, passing as first input the name of the file and as second the opening mode. As mentioned, the name of the file will be “/test.txt” and, since we want to open the file for writing content, we should pass the FILE_WRITE constant as second parameter.

As output, the open method will return an object of class File, which we will use to write the content.

So, after checking if the file was correctly opened, we will write some content to the file, using the print method. This method returns the number of bytes written to the file, so we can use that value to perform an error check and confirm the content was written.

After the writing procedure, we will close the file.

File file = SPIFFS.open("/test.txt", FILE_WRITE);

if (!file) {
    Serial.println("There was an error opening the file for writing");
    return;
}

if (file.print("TEST")) {
    Serial.println("File was written");
} else {
    Serial.println("File write failed");
}

file.close();

Now we will check if this file exists simply by calling the exists method on the SPIFFS extern variable, passing as input the name of the file. The method will return true if the file exists and false otherwise.

We will test first with our previously created “/test.txt” file. Since the file exists, the expected result is true.

Serial.println(SPIFFS.exists("/test.txt"));

Finally, we will try the same exists method with a non-existing file. Naturally, the result should be false.

Serial.println(SPIFFS.exists("/nonexisting.txt"));

The final source code can be seen below.

#include "SPIFFS.h"

void setup() {

  Serial.begin(115200);

  if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }

  File file = SPIFFS.open("/test.txt", FILE_WRITE);

  if (!file) {
    Serial.println("There was an error opening the file for writing");
    return;
  }

  if (file.print("TEST")) {
    Serial.println("File was written");
  } else {
    Serial.println("File write failed");
  }

  file.close();

  Serial.println(SPIFFS.exists("/test.txt"));
  Serial.println(SPIFFS.exists("/nonexisting.txt"));

}

void loop() {}

Testing the code

To test the code, simply compile it and upload it to your ESP32 device. Once the procedure finishes, open the Arduino IDE serial monitor. You should get an output similar to figure 1.

As can be seen below, for the first file name (the one that exists in the file system because we previously created it), the exists method returns true (1 corresponds to true). On the other hand, for the second file name tested, which did not exist in the file system, the method returns false (0 corresponds to false).

Arduino IDE serial monitor with result of testing if files exist in the ESP32 file system

Figure 1 – Output of the program.

Advertisements

2 Replies to “ESP32 Arduino SPIFFS: testing if file exists”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s