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.

78 lines
2.2KB

  1. /*
  2. * This sketch demonstrates use of SdFile::rename()
  3. * and SdFat::rename().
  4. */
  5. #include <SdFat.h>
  6. // SD chip select pin
  7. const uint8_t chipSelect = SS;
  8. // file system
  9. SdFat sd;
  10. // Serial print stream
  11. ArduinoOutStream cout(Serial);
  12. //------------------------------------------------------------------------------
  13. // store error strings in flash to save RAM
  14. #define error(s) sd.errorHalt_P(PSTR(s))
  15. //------------------------------------------------------------------------------
  16. void setup() {
  17. Serial.begin(9600);
  18. while (!Serial) {} // wait for Leonardo
  19. cout << pstr("Insert an empty SD. Type any character to start.") << endl;
  20. while (Serial.read() <= 0) {}
  21. delay(400); // catch Due reset problem
  22. // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  23. // breadboards. use SPI_FULL_SPEED for better performance.
  24. if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
  25. // create a file and write one line to the file
  26. SdFile file("NAME1.TXT", O_WRITE | O_CREAT);
  27. if (!file.isOpen()) error("NAME1");
  28. file.println("A test line for NAME1.TXT");
  29. // rename the file NAME2.TXT and add a line.
  30. // sd.vwd() is the volume working directory, root.
  31. if (!file.rename(sd.vwd(), "NAME2.TXT")) error("NAME2");
  32. file.println("A test line for NAME2.TXT");
  33. // list files
  34. cout << pstr("------") << endl;
  35. sd.ls(LS_R);
  36. // make a new directory - "DIR1"
  37. if (!sd.mkdir("DIR1")) error("DIR1");
  38. // move file into DIR1, rename it NAME3.TXT and add a line
  39. if (!file.rename(sd.vwd(), "DIR1/NAME3.TXT")) error("NAME3");
  40. file.println("A line for DIR1/NAME3.TXT");
  41. // list files
  42. cout << pstr("------") << endl;
  43. sd.ls(LS_R);
  44. // make directory "DIR2"
  45. if (!sd.mkdir("DIR2")) error("DIR2");
  46. // close file before rename(oldPath, newPath)
  47. file.close();
  48. // move DIR1 into DIR2 and rename it DIR3
  49. if (!sd.rename("DIR1", "DIR2/DIR3")) error("DIR2/DIR3");
  50. // open file for append in new location and add a line
  51. if (!file.open("DIR2/DIR3/NAME3.TXT", O_WRITE | O_APPEND)) {
  52. error("DIR2/DIR3/NAME3.TXT");
  53. }
  54. file.println("A line for DIR2/DIR3/NAME3.TXT");
  55. // list files
  56. cout << pstr("------") << endl;
  57. sd.ls(LS_R);
  58. cout << pstr("Done") << endl;
  59. }
  60. void loop() {}