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.6KB

10 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
10 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * This program 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 << F("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)) {
  26. sd.initErrorHalt();
  27. }
  28. // Remove file/dirs from previous run.
  29. if (sd.exists("dir2/DIR3/NAME3.txt")) {
  30. cout << F("Removing /dir2/DIR3/NAME3.txt") << endl;
  31. if (!sd.remove("dir2/DIR3/NAME3.txt") ||
  32. !sd.rmdir("dir2/DIR3/") ||
  33. !sd.rmdir("dir2/")) {
  34. error("remove/rmdir failed");
  35. }
  36. }
  37. // create a file and write one line to the file
  38. SdFile file("Name1.txt", O_WRITE | O_CREAT);
  39. if (!file.isOpen()) {
  40. error("Name1.txt");
  41. }
  42. file.println("A test line for Name1.txt");
  43. // rename the file name2.txt and add a line.
  44. // sd.vwd() is the volume working directory, root.
  45. if (!file.rename(sd.vwd(), "name2.txt")) {
  46. error("name2.txt");
  47. }
  48. file.println("A test line for name2.txt");
  49. // list files
  50. cout << F("------") << endl;
  51. sd.ls(LS_R);
  52. // make a new directory - "Dir1"
  53. if (!sd.mkdir("Dir1")) {
  54. error("Dir1");
  55. }
  56. // move file into Dir1, rename it NAME3.txt and add a line
  57. if (!file.rename(sd.vwd(), "Dir1/NAME3.txt")) {
  58. error("NAME3.txt");
  59. }
  60. file.println("A line for Dir1/NAME3.txt");
  61. // list files
  62. cout << F("------") << endl;
  63. sd.ls(LS_R);
  64. // make directory "dir2"
  65. if (!sd.mkdir("dir2")) {
  66. error("dir2");
  67. }
  68. // close file before rename(oldPath, newPath)
  69. file.close();
  70. // move Dir1 into dir2 and rename it DIR3
  71. if (!sd.rename("Dir1", "dir2/DIR3")) {
  72. error("dir2/DIR3");
  73. }
  74. // open file for append in new location and add a line
  75. if (!file.open("dir2/DIR3/NAME3.txt", O_WRITE | O_APPEND)) {
  76. error("dir2/DIR3/NAME3.txt");
  77. }
  78. file.println("A line for dir2/DIR3/NAME3.txt");
  79. file.close();
  80. // list files
  81. cout << F("------") << endl;
  82. sd.ls(LS_R);
  83. cout << F("Done") << endl;
  84. }
  85. void loop() {}