rename.ino 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 max SPI clock for an SD. Reduce SPI_CLOCK if errors occur.
  25. #define SPI_CLOCK SD_SCK_MHZ(50)
  26. // Try to select the best SD card configuration.
  27. #if HAS_SDIO_CLASS
  28. #define SD_CONFIG SdioConfig(FIFO_SDIO)
  29. #elif ENABLE_DEDICATED_SPI
  30. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SPI_CLOCK)
  31. #else // HAS_SDIO_CLASS
  32. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, SHARED_SPI, SPI_CLOCK)
  33. #endif // HAS_SDIO_CLASS
  34. #if SD_FAT_TYPE == 0
  35. SdFat sd;
  36. File file;
  37. #elif SD_FAT_TYPE == 1
  38. SdFat32 sd;
  39. File32 file;
  40. #elif SD_FAT_TYPE == 2
  41. SdExFat sd;
  42. ExFile file;
  43. #elif SD_FAT_TYPE == 3
  44. SdFs sd;
  45. FsFile file;
  46. #else // SD_FAT_TYPE
  47. #error Invalid SD_FAT_TYPE
  48. #endif // SD_FAT_TYPE
  49. // Serial print stream
  50. ArduinoOutStream cout(Serial);
  51. //------------------------------------------------------------------------------
  52. // store error strings in flash to save RAM
  53. #define error(s) sd.errorHalt(&Serial, F(s))
  54. //------------------------------------------------------------------------------
  55. void setup() {
  56. Serial.begin(9600);
  57. // Wait for USB Serial
  58. while (!Serial) {
  59. yield();
  60. }
  61. cout << F("Insert an empty SD. Type any character to start.") << endl;
  62. while (!Serial.available()) {
  63. yield();
  64. }
  65. // Initialize at the highest speed supported by the board that is
  66. // not over 50 MHz. Try a lower speed if SPI errors occur.
  67. if (!sd.begin(SD_CONFIG)) {
  68. sd.initErrorHalt(&Serial);
  69. }
  70. // Remove file/dirs from previous run.
  71. if (sd.exists("dir2/DIR3/NAME3.txt")) {
  72. cout << F("Removing /dir2/DIR3/NAME3.txt") << endl;
  73. if (!sd.remove("dir2/DIR3/NAME3.txt") ||
  74. !sd.rmdir("dir2/DIR3/") ||
  75. !sd.rmdir("dir2/")) {
  76. error("remove/rmdir failed");
  77. }
  78. }
  79. // create a file and write one line to the file
  80. if (!file.open("Name1.txt", O_WRONLY | O_CREAT)) {
  81. error("Name1.txt");
  82. }
  83. file.println("A test line for Name1.txt");
  84. // rename the file name2.txt and add a line.
  85. if (!file.rename("name2.txt")) {
  86. error("name2.txt");
  87. }
  88. file.println("A test line for name2.txt");
  89. // list files
  90. cout << F("------") << endl;
  91. sd.ls(LS_R);
  92. // make a new directory - "Dir1"
  93. if (!sd.mkdir("Dir1")) {
  94. error("Dir1");
  95. }
  96. // move file into Dir1, rename it NAME3.txt and add a line
  97. if (!file.rename("Dir1/NAME3.txt")) {
  98. error("NAME3.txt");
  99. }
  100. file.println("A line for Dir1/NAME3.txt");
  101. // list files
  102. cout << F("------") << endl;
  103. sd.ls(LS_R);
  104. // make directory "dir2"
  105. if (!sd.mkdir("dir2")) {
  106. error("dir2");
  107. }
  108. // close file before rename(oldPath, newPath)
  109. file.close();
  110. // move Dir1 into dir2 and rename it DIR3
  111. if (!sd.rename("Dir1", "dir2/DIR3")) {
  112. error("dir2/DIR3");
  113. }
  114. // open file for append in new location and add a line
  115. if (!file.open("dir2/DIR3/NAME3.txt", O_WRONLY | O_APPEND)) {
  116. error("dir2/DIR3/NAME3.txt");
  117. }
  118. file.println("A line for dir2/DIR3/NAME3.txt");
  119. file.close();
  120. // list files
  121. cout << F("------") << endl;
  122. sd.ls(LS_R);
  123. cout << F("Done") << endl;
  124. }
  125. void loop() {}