In this esp32 tutorial we will check how to rename a file on 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 esp32 tutorial we will check how to rename a file on the ESP32 SPIFFS file system, using the Arduino core.
The code
As usual, we will start our code by including the SPIFFS.h library.
#include "SPIFFS.h"
Then we are going to declare an auxiliary function called listAllFiles that will allow us to list all the files in the SPIFFS file system. With this function, we will be able to print the state of the file system before and after we rename the file.
For a detailed tutorial that explains the procedure, please check here.
void listAllFiles(){
File root = SPIFFS.open("/");
File file = root.openNextFile();
while(file){
Serial.print("FILE: ");
Serial.println(file.name());
file = root.openNextFile();
}
}
Moving on to the Arduino setup function, we will first open a serial connection and then mount the SPIFFS file system.
Serial.begin(115200);
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
After that, we will create a file called “test.txt” in the SPIFFS file system. This will be the file that we will later rename. For a detailed explanation on how to create a file, please check this post.
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("some content")) {
Serial.println("File was written");
} else {
Serial.println("File write failed");
}
file.close();
After creating the file, we will call our listAllFiles function, to make sure the file was correctly created and appears in the list of files of the file system.
Serial.println("\n\n---BEFORE RENAMING---");
listAllFiles();
Then, to rename a file, we simply need to call the rename method on the SPIFFS object. As first input, the method receives the full path of the file, as a string. As second input, it receives the new path of the file, to be renamed, also as a string.
Note that since SPIFFS is a flat file system, there is no support for directories [1]. Thus, we can pass any path we want with multiple “/” characters representing directories (as long as it doesn’t exceed the maximum limit of 31 characters). The path used will be the actual name that will be assigned to the file.
Nonetheless, for this tutorial, we will simply rename the file to “/renamed.txt“.
SPIFFS.rename("/test.txt", "/renamed.txt");
To finalize, we will print the list of files again. The list should no longer show a file called “/test.txt” but rather a file called “/renamed.txt“.
Serial.println("\n\n---AFTER RENAMING---");
listAllFiles();
The final source code can be seen below.
#include "SPIFFS.h"
void listAllFiles(){
File root = SPIFFS.open("/");
File file = root.openNextFile();
while(file){
Serial.print("FILE: ");
Serial.println(file.name());
file = root.openNextFile();
}
}
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("some content")) {
Serial.println("File was written");
} else {
Serial.println("File write failed");
}
file.close();
Serial.println("\n\n---BEFORE RENAMING---");
listAllFiles();
SPIFFS.rename("/test.txt", "/renamed.txt");
Serial.println("\n\n---AFTER RENAMING---");
listAllFiles();
}
void loop() {}
Testing the code
To test the code, simply compile it and upload it to your device, using the Arduino IDE. When the procedure finishes, open the Arduino IDE serial monitor. You should get an output similar to figure 1.
As can be seen, the first time we print the list of files, a file called “/test.txt” is shown. After we rename it and print the list again, the file is indeed renamed to “/renamed.txt“.
Reference:
https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/storage/spiffs.html