Chat freely about anything...

User avatar
By Sirquil
#64253
I've just tested the SPIFFS.rename() and it is working properly.
Maybe something else in your code that you didn't tested properly. - See more at: posting.php?mode=reply&f=6&t=14299#sthash.w2e538AQ.dpuf


I do not understand; when I list the files with listFile function, the renamed file is not listed.
Serial.println(logname.c_str() prints the String filename. Filename produced is "LOG325.TXT" is correct for month and date.

Image

William
Attachments
ESP8266 --Data Logger and Dynamic Web Server, updated 3/26/2017.
(8.95 KiB) Downloaded 175 times
User avatar
By martinayotte
#64254 Oh ! I figured out something looking at your code :
You open the file before renaming it, it should not be open !
It is a bit like the file becomes locked, and rename() probably fail because of that.
Why do you need to open it ? Simply rename it, and then open the renamed version ...
(If you open it, just to write something into it, do the writes, then close it and then rename it
User avatar
By Sirquil
#64260 Removed the File log open and close; same result, "LOG325.TXT" is not listed by listFile function.

Code: Select allvoid fileStore()   //If 7th day of week, rename "log.txt" to ("log" + month + day + ".txt") and create new, empty "log.txt"
{

     getDateTime();

     // rename the file "LOG.TXT"
     String logname;
     logname = "LOG";
     logname += Clock.getMonth(Century);
     logname += Clock.getDate();
     logname += ".TXT";
     SPIFFS.rename("LOG.TXT", logname.c_str());
     Serial.println("Gets Here 2");

     //For troubleshooting
     Serial.println(logname.c_str());

     // create a new "log.txt" file for appended writing
     File log = SPIFFS.open("LOG.TXT", "a");

     if (!log)
     {
          Serial.println("file open failed");
     }
     log.println("");
     log.close();

     Serial.println("");
     Serial.println("New LOG.TXT created");

}


This is the function fileStore from the Arduino Mega 2560 that worked:

Code: Select allvoid fileStore()   //If 7th day of week, rename "log.txt" to ("log" + month + day + ".txt") and create new, empty "log.txt"
{

     // create a file and write one line to the file
     SdFile logFile("log.txt", O_WRITE | O_CREAT );

     if (!logFile.isOpen())
     {
          error("log.txt --new -open");
     }

     // rename the file log.txt
     // sd.vwd() is the volume working directory, root.

     logFileName = "";
     logFileName = "log";
     logFileName += (RTCTimedEvent.time.month);
     logFileName += (RTCTimedEvent.time.day);
     logFileName += ".txt";
     //Serial.println(logFileName.c_str());

     if(sd.exists("log.txt"))
     {
          logFile.rename(sd.vwd(), logFileName.c_str());
          logFile.close();
     }
     else
     {
          exit;
     }

     // create a new "log.txt" file for appended writing
     logFile.open("log.txt", O_WRITE | O_CREAT | O_APPEND);
     logFile.println("");
     logFile.close();

     Serial.begin(115200);

     Serial.println("");
     Serial.println("New LOG.TXT created");

     // list files
     cout << pstr("------") << endl;
     sd.ls(LS_R);

     Serial.flush();
     Serial.end();

}


William