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.

rename.ino 2.2KB

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