SPIFFS file operate question need help please

Hi everyone.
The below code write a file and rename it in SPIFFS, my question is can use it to rename any file's name or not?
Thanks
Adam

#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() {}

Would it make sense if you could only rename files called test.txt?

What does the Doc say?

1 Like

Thank you J-M-L.
I didn't find useful info. of doc.
the test.txt is the code designed. and I added some thing like, and the code wait for type input that is a file listed.

while (Serial.available() == 0) {}     //wait for data available
    ReceiveName = Serial.readString();
    ReceiveName.trim();

I don't know why not work.

Revised:

String ReceiveName;

#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();

  ////////////////////////
  while (Serial.available() == 0) {}     //wait for data available
  ReceiveName = Serial.readString();
  ReceiveName.trim();

   Serial.println("ReceiveName =");
   Serial.println(ReceiveName);
  ////////////////////////

  //// SPIFFS.rename("/test.txt", "/renamed.txt");
  SPIFFS.rename(ReceiveName.c_str(), "/renamed.txt");

  Serial.println("\n\n---AFTER RENAMING---");
  listAllFiles();

}

void loop() {}

You need to use SPIFFS.begin(); to mount spiffs.

Then you can write like this:

SPIFFS.begin();

File file = SPIFFS.open("/test.txt", "w");
file.print("some text");
file.close();

That's all.

1 Like

The file you created and needs to be renamed is named test.txt

You get the new name from the Serial monitor

So the command should be

SPIFFS.rename("/test.txt", ReceiveName.c_str() );

Make sure you enter the leading / in the new name or add it programmatically before calling rename()

1 Like

GREAT!
Thank you.
is it possible to make COPY/PASTE file operate without use web serve?

I don't know what you mean by copy/paste

1 Like

I mean to copy a file from SPIFFS to SD card, or reverse.

you have to move the bytes programatically from one to the other. there is no OS doing that for you

1 Like

Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.