You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.2KB

  1. /*
  2. * This sketch will remove the files and directories
  3. * created by the SdFatMakeDir.pde sketch.
  4. *
  5. * Performance is erratic due to the large number
  6. * of flash erase operations caused by many random
  7. * writes to file structures.
  8. */
  9. #include <SdFat.h>
  10. #include <SdFatUtil.h>
  11. const uint8_t SD_CHIP_SELECT = SS;
  12. SdFat sd;
  13. // store error strings in flash to save RAM
  14. #define error(s) sd.errorHalt_P(PSTR(s))
  15. /*
  16. * remove all files in dir.
  17. */
  18. void deleteFiles(SdBaseFile* dir) {
  19. char name[13];
  20. SdFile file;
  21. // open and delete files
  22. for (uint16_t n = 0; ; n++){
  23. sprintf(name, "%u.TXT", n);
  24. // open start time
  25. uint32_t t0 = millis();
  26. // assume done if open fails
  27. if (!file.open(dir, name, O_WRITE)) return;
  28. // open end time and remove start time
  29. uint32_t t1 = millis();
  30. if (!file.remove()) error("file.remove failed");
  31. // remove end time
  32. uint32_t t2 = millis();
  33. PgmPrint("RM ");
  34. Serial.print(n);
  35. Serial.write(' ');
  36. // open time
  37. Serial.print(t1 - t0);
  38. Serial.write(' ');
  39. // remove time
  40. Serial.println(t2 - t1);
  41. }
  42. }
  43. void setup() {
  44. Serial.begin(9600);
  45. while (!Serial) {} // wait for Leonardo
  46. PgmPrintln("Type any character to start");
  47. while (Serial.read() <= 0) {}
  48. delay(200); // Catch Due reset problem
  49. // initialize the SD card at SPI_FULL_SPEED for best performance.
  50. // try SPI_HALF_SPEED if bus errors occur.
  51. if (!sd.begin(SD_CHIP_SELECT, SPI_FULL_SPEED)) sd.initErrorHalt();
  52. // delete files in root if FAT32
  53. if (sd.vol()->fatType() == 32) {
  54. PgmPrintln("Remove files in root");
  55. deleteFiles(sd.vwd());
  56. }
  57. // open SUB1 and delete files
  58. SdFile sub1;
  59. if (!sub1.open("SUB1", O_READ)) error("open SUB1 failed");
  60. PgmPrintln("Remove files in SUB1");
  61. deleteFiles(&sub1);
  62. // open SUB2 and delete files
  63. SdFile sub2;
  64. if (!sub2.open(&sub1, "SUB2", O_READ)) error("open SUB2 failed");
  65. PgmPrintln("Remove files in SUB2");
  66. deleteFiles(&sub2);
  67. // remove SUB2
  68. if (!sub2.rmDir()) error("sub2.rmDir failed");
  69. PgmPrintln("SUB2 removed");
  70. // remove SUB1
  71. if (!sub1.rmDir()) error("sub1.rmDir failed");
  72. PgmPrintln("SUB1 removed");
  73. PgmPrintln("Done");
  74. }
  75. void loop() { }