rename.ino 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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") || !sd.rmdir("dir2/DIR3/") ||
  74. !sd.rmdir("dir2/")) {
  75. error("remove/rmdir failed");
  76. }
  77. }
  78. // create a file and write one line to the file
  79. if (!file.open("Name1.txt", O_WRONLY | O_CREAT)) {
  80. error("Name1.txt");
  81. }
  82. file.println("A test line for Name1.txt");
  83. // rename the file name2.txt and add a line.
  84. if (!file.rename("name2.txt")) {
  85. error("name2.txt");
  86. }
  87. file.println("A test line for name2.txt");
  88. // list files
  89. cout << F("------") << endl;
  90. sd.ls(LS_R);
  91. // make a new directory - "Dir1"
  92. if (!sd.mkdir("Dir1")) {
  93. error("Dir1");
  94. }
  95. // move file into Dir1, rename it NAME3.txt and add a line
  96. if (!file.rename("Dir1/NAME3.txt")) {
  97. error("NAME3.txt");
  98. }
  99. file.println("A line for Dir1/NAME3.txt");
  100. // list files
  101. cout << F("------") << endl;
  102. sd.ls(LS_R);
  103. // make directory "dir2"
  104. if (!sd.mkdir("dir2")) {
  105. error("dir2");
  106. }
  107. // close file before rename(oldPath, newPath)
  108. file.close();
  109. // move Dir1 into dir2 and rename it DIR3
  110. if (!sd.rename("Dir1", "dir2/DIR3")) {
  111. error("dir2/DIR3");
  112. }
  113. // open file for append in new location and add a line
  114. if (!file.open("dir2/DIR3/NAME3.txt", O_WRONLY | O_APPEND)) {
  115. error("dir2/DIR3/NAME3.txt");
  116. }
  117. file.println("A line for dir2/DIR3/NAME3.txt");
  118. file.close();
  119. // list files
  120. cout << F("------") << endl;
  121. sd.ls(LS_R);
  122. cout << F("Done") << endl;
  123. }
  124. void loop() {}