ESP32 Arduino SPIFFS: Getting the size of a file

In this tutorial we will check how to obtain the size of the file from the ESP32 SPIFFS file system, using the Arduino core. The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.

 

Introduction

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.


Related Posts

Advertisements
This entry was posted in ESP32 and tagged , , , . Bookmark the permalink.

6 Responses to ESP32 Arduino SPIFFS: Getting the size of a file

  1. Mark says:

    Is it possible to download SPIFFS files?

    Liked by 1 person

  2. Pingback: ESP32 SPIFFS: Append content to file | techtutorialsx

  3. Pingback: ESP32 Arduino SPIFFS: Append content to file | techtutorialsx

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