0

$USD

$USD
TUTORIALS ESP32

ESP32 Arduino Tutorial SPIFFS:6. Getting the size of a file

DFRobot Nov 23 2018 1491

In this tutorial we will check how to obtain the size of the file from the ESP32 SPIFFS file system, using the Arduino core.

For an introduction on how to write to a file, please check here. For a tutorial on how to read from a file, please check this other post.

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


T
he code

As we did in the previous posts, we need to include the SPIFFS.h library. That way, we will have access to the SPIFFS extern variable, which we will use to interact with the file system.

#include "SPIFFS.h"

Moving on to the Arduino setup, we start by opening a serial connection, so we can later output the results of our program. We will also mount the SPIFFS file system, using the begin method of the already mentioned SPIFFS extern variable.

Serial.begin(115200);

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

Now we will write some content to a file. Note that if you have already created and written to a file while following one of the previous tutorials, then you can skip this part if you want and use that file.

To open the file, we need to call the open method on the SPIFFS variable, passing as first input the name of the file to open and as second the opening mode. We will write to a file called “test.txt” and open the file in writing mode by passing as second parameter the constant FILE_WRITE.

This will return an object of class File and since this class overrides the C++ Boolean operator, we can check if the opening operation was successful simply by using an IF condition.

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

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

If the file was opened with success, we can proceed with writing some content to it. We will declare an arbitrary string and print its size before writing it to the file. Since I’m using a const char * rather than the Arduino String class, it is possible to obtain its size in bytes using the strlen function.

const char * content = "Testing content!";

Serial.print("Content length: ");
Serial.println(strlen(content));

We then proceed with writing the content to the file and doing an error check to confirm that the content was written.

We finish the writing procedure by calling the close method on our file object.

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

file.close();

Now that we have the file with some content, we will open it in read mode, since we are no longer going to modify it but rather simply read its size.

We do that again by using the open method of the SPIFFS variable but, this time, we don’t need to explicitly pass the opening mode since that is an optional argument that defaults to reading mode (the one we want). This will again return an object of class File.

After that, we check again if the file was open successfully before trying to use it.

File file2 = SPIFFS.open("/test.txt");

if(!file2){
    Serial.println("Failed to open file for reading");
    return;
}

On success, we can obtain the size of the file simply by calling the size method on our File object. This method takes no arguments and returns the size of the file, in bytes.

Serial.print("File size: ");
Serial.println(file2.size());

To finalize the procedure, we close the file.

file2.close();

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;
    }

    const char * content = "Testing content!";

    Serial.print("Content length: ");
    Serial.println(strlen(content));

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

    file.close();

    File file2 = SPIFFS.open("/test.txt");

    if(!file2){
        Serial.println("Failed to open file for reading");
        return;
    }

    Serial.print("File size: ");

    Serial.println(file2.size());

    file2.close();
}

void loop() {}


Testing the code

To test the previous code, simply compile it and upload it to your device using the Arduino IDE. Upon completion, open the Arduino IDE serial monitor. You should get an output similar to figure 1, which shows both the size of the content written and the size of the file, which match.

ESP32 Arduino SPIFFS file system get size of file.png

Figure 1 – Getting the size of the file in the ESP32 SPIFFS file system.


DFRobot supply lots of esp32 arduino tutorials and esp32 projects for makers to learn.