SPIFFS_Support.ino 2.4 KB

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