0

$USD

$USD
TUTORIALS ESP32

ESP32 Arduino Tutorial: 23. FAT file system - Checking if file exists

DFRobot Jan 30 2019 1599

Introduction

In this tutorial we will learn how to check if a file exists in the ESP32 FAT file system, using the file name.

If you haven’t yet worked with the FAT file system before on the ESP32, please consult this previous tutorial, which explains in detail how to get started.

To guarantee that we have a file on the file system and confirm its existence, we will first write a file by following the procedure covered here. If you already have a file in your file system, you can use it for testing and skip the file writing procedure.

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

The code

As usual, we get started by including the library needed to interact with the FAT file system of the ESP32. This library is the FFat.h and, by including it, we will have access to an extern variable called FFat, which exposes the methods needed to interact with the file system.

#include "FFat.h"

Moving to the Arduino setup function, we start by opening a serial connection, to be able to output some messages from our program. Followed by that, we will mount the file system, which is the initial procedure we need to execute before interacting with it.

Serial.begin(115200);

if(!FFat.begin(true)){
     Serial.println("Mount Failed");
     return;
}

Serial.println("File system mounted");

Next we will write a file called “/testfatexists.txt”, so we can later test for its existence and compare the result against a non-existing file.

As we covered before, we first need to open the file in writing mode, using the open method of the FFat extern variable. As first input we need to pass the already mentioned file name and as second input we need to pass the FILE_WRITE constant, which indicates we will open the file in writing mode.

As output, the open method returns an object of class File. Since this class overloads the C++Boolean operator, we can enclose the returned object in an IF condition to check if it was correctly opened.

Next we write the content by calling the print method on our File object, passing as input some arbitrary content that will be written to the file. Since we are not going to read the content, you can write whatever content you want to the file.

Since the previous method call returns the number of bytes written to the file, we also enclose it in an IF condition to confirm the content was written correctly.

To finalize the file writing procedure we need to call the close methods on the File object. This method takes no arguments.

File file = FFat.open("/testfatexists.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();

To confirm if a file exists in the FAT file system, we simply need to call the exists method on the FFat extern variable, passing as input the name of the file we want to test. The method returns true if the file exists and false otherwise. Note that the method name is exactly the same we use in the SPIFFS file system, as can be seen here.

We will do this to both the name of the previously created file and using a name of a non-existing file.

Serial.println(FFat.exists("/testfatexists.txt"));
Serial.println(FFat.exists("/nonexisting.txt"));

The final code can be seen below.

#include "FFat.h"

void setup(){

    Serial.begin(115200);

    if(!FFat.begin(true)){
        Serial.println("Mount Failed");
        return;
    }

    Serial.println("File system mounted");

    File file = FFat.open("/testfatexists.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(FFat.exists("/testfatexists.txt"));
    Serial.println(FFat.exists("/nonexisting.txt"));

}

void loop(){}

Testing the code

In order to test the code from the previous section, compile it and upload it to your ESP32 using the Arduino IDE. Once the procedure is complete, open the Arduino IDE serial monitor.

You should get an output similar to figure 1. As can be seen, for the existing file we obtain the value 1 (which means true) and for the non-existing file we get the value 0 (which means false).

Output of the program that checks if a file exists in the ESP32 FAT file system

Figure 1 – Output of the file existence checking program.