SPIFFS_Support.ino 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #if defined(ESP32)
  2. #include "FS.h"
  3. #include "SPIFFS.h"
  4. void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
  5. Serial.printf("Listing directory: %s\r\n", dirname);
  6. File root = fs.open(dirname);
  7. if (!root) {
  8. Serial.println("- failed to open directory");
  9. return;
  10. }
  11. if (!root.isDirectory()) {
  12. Serial.println(" - not a directory");
  13. return;
  14. }
  15. File file = root.openNextFile();
  16. while (file) {
  17. if (file.isDirectory()) {
  18. Serial.print(" DIR : ");
  19. Serial.println(file.name());
  20. if (levels) {
  21. listDir(fs, file.name(), levels - 1);
  22. }
  23. } else {
  24. Serial.print(" FILE: ");
  25. Serial.print(file.name());
  26. Serial.print("\tSIZE: ");
  27. Serial.println(file.size());
  28. }
  29. file = root.openNextFile();
  30. }
  31. }
  32. void listFiles()
  33. {
  34. listDir(SPIFFS, "/", 0);
  35. }
  36. #elif defined(ESP8266)
  37. /*====================================================================================
  38. This sketch contains support functions for the ESP6266 SPIFFS filing system
  39. Created by Bodmer 15th Jan 2017
  40. ==================================================================================*/
  41. //====================================================================================
  42. // Print a SPIFFS directory list (root directory)
  43. //====================================================================================
  44. void listFiles(void) {
  45. Serial.println();
  46. Serial.println("SPIFFS files found:");
  47. fs::Dir dir = SPIFFS.openDir(""); // Root directory
  48. String line = "=====================================";
  49. uint32_t totalBytes = 0;
  50. Serial.println(line);
  51. Serial.println(" File name Size");
  52. Serial.println(line);
  53. while (dir.next()) {
  54. String fileName = dir.fileName();
  55. Serial.print(fileName);
  56. int spaces = 25 - fileName.length(); // Tabulate nicely
  57. while (spaces--) Serial.print(" ");
  58. fs::File f = dir.openFile("r");
  59. Serial.print(f.size()); Serial.println(" bytes");
  60. totalBytes += f.size();
  61. }
  62. Serial.println(); Serial.print("Total = ");
  63. int spaces = 25 - 8; // Tabulate nicely
  64. while (spaces--) Serial.print(" ");
  65. Serial.print(totalBytes); Serial.println(" bytes");
  66. Serial.println(line);
  67. Serial.println();
  68. delay(1000);
  69. }
  70. //====================================================================================
  71. #else
  72. void listFiles()
  73. {
  74. Serial.println("SPIFFS listFiles() not implemented");
  75. }
  76. #endif