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.

144 line
3.5KB

  1. /*
  2. * This program demonstrates use of rename().
  3. */
  4. #include "SdFat.h"
  5. #include "sdios.h"
  6. // SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
  7. // 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
  8. #define SD_FAT_TYPE 0
  9. /*
  10. Change the value of SD_CS_PIN if you are using SPI and
  11. your hardware does not use the default value, SS.
  12. Common values are:
  13. Arduino Ethernet shield: pin 4
  14. Sparkfun SD shield: pin 8
  15. Adafruit SD shields and modules: pin 10
  16. */
  17. // SDCARD_SS_PIN is defined for the built-in SD on some boards.
  18. #ifndef SDCARD_SS_PIN
  19. const uint8_t SD_CS_PIN = SS;
  20. #else // SDCARD_SS_PIN
  21. // Assume built-in SD is used.
  22. const uint8_t SD_CS_PIN = SDCARD_SS_PIN;
  23. #endif // SDCARD_SS_PIN
  24. // Try to select the best SD card configuration.
  25. #if HAS_SDIO_CLASS
  26. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  27. #elif ENABLE_DEDICATED_SPI
  28. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI)
  29. #else // HAS_SDIO_CLASS
  30. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI)
  31. #endif // HAS_SDIO_CLASS
  32. #if SD_FAT_TYPE == 0
  33. SdFat sd;
  34. File file;
  35. #elif SD_FAT_TYPE == 1
  36. SdFat32 sd;
  37. File32 file;
  38. #elif SD_FAT_TYPE == 2
  39. SdExFat sd;
  40. ExFile file;
  41. #elif SD_FAT_TYPE == 3
  42. SdFs sd;
  43. FsFile file;
  44. #else // SD_FAT_TYPE
  45. #error Invalid SD_FAT_TYPE
  46. #endif // SD_FAT_TYPE
  47. // Serial print stream
  48. ArduinoOutStream cout(Serial);
  49. //------------------------------------------------------------------------------
  50. // store error strings in flash to save RAM
  51. #define error(s) sd.errorHalt(&Serial, F(s))
  52. //------------------------------------------------------------------------------
  53. void setup() {
  54. Serial.begin(9600);
  55. // Wait for USB Serial
  56. while (!Serial) {
  57. SysCall::yield();
  58. }
  59. cout << F("Insert an empty SD. Type any character to start.") << endl;
  60. while (!Serial.available()) {
  61. SysCall::yield();
  62. }
  63. // Initialize at the highest speed supported by the board that is
  64. // not over 50 MHz. Try a lower speed if SPI errors occur.
  65. if (!sd.begin(SD_CONFIG)) {
  66. sd.initErrorHalt(&Serial);
  67. }
  68. // Remove file/dirs from previous run.
  69. if (sd.exists("dir2/DIR3/NAME3.txt")) {
  70. cout << F("Removing /dir2/DIR3/NAME3.txt") << endl;
  71. if (!sd.remove("dir2/DIR3/NAME3.txt") ||
  72. !sd.rmdir("dir2/DIR3/") ||
  73. !sd.rmdir("dir2/")) {
  74. error("remove/rmdir failed");
  75. }
  76. }
  77. // create a file and write one line to the file
  78. if (!file.open("Name1.txt", O_WRONLY | O_CREAT)) {
  79. error("Name1.txt");
  80. }
  81. file.println("A test line for Name1.txt");
  82. // rename the file name2.txt and add a line.
  83. if (!file.rename("name2.txt")) {
  84. error("name2.txt");
  85. }
  86. file.println("A test line for name2.txt");
  87. // list files
  88. cout << F("------") << endl;
  89. sd.ls(LS_R);
  90. // make a new directory - "Dir1"
  91. if (!sd.mkdir("Dir1")) {
  92. error("Dir1");
  93. }
  94. // move file into Dir1, rename it NAME3.txt and add a line
  95. if (!file.rename("Dir1/NAME3.txt")) {
  96. error("NAME3.txt");
  97. }
  98. file.println("A line for Dir1/NAME3.txt");
  99. // list files
  100. cout << F("------") << endl;
  101. sd.ls(LS_R);
  102. // make directory "dir2"
  103. if (!sd.mkdir("dir2")) {
  104. error("dir2");
  105. }
  106. // close file before rename(oldPath, newPath)
  107. file.close();
  108. // move Dir1 into dir2 and rename it DIR3
  109. if (!sd.rename("Dir1", "dir2/DIR3")) {
  110. error("dir2/DIR3");
  111. }
  112. // open file for append in new location and add a line
  113. if (!file.open("dir2/DIR3/NAME3.txt", O_WRONLY | O_APPEND)) {
  114. error("dir2/DIR3/NAME3.txt");
  115. }
  116. file.println("A line for dir2/DIR3/NAME3.txt");
  117. file.close();
  118. // list files
  119. cout << F("------") << endl;
  120. sd.ls(LS_R);
  121. cout << F("Done") << endl;
  122. }
  123. void loop() {}