您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

107 行
2.6KB

  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. // Wait for USB Serial
  20. while (!Serial) {
  21. SysCall::yield();
  22. }
  23. cout << F("Insert an empty SD. Type any character to start.") << endl;
  24. while (!Serial.available()) {
  25. SysCall::yield();
  26. }
  27. // Initialize at the highest speed supported by the board that is
  28. // not over 50 MHz. Try a lower speed if SPI errors occur.
  29. if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  30. sd.initErrorHalt();
  31. }
  32. // Remove file/dirs from previous run.
  33. if (sd.exists("dir2/DIR3/NAME3.txt")) {
  34. cout << F("Removing /dir2/DIR3/NAME3.txt") << endl;
  35. if (!sd.remove("dir2/DIR3/NAME3.txt") ||
  36. !sd.rmdir("dir2/DIR3/") ||
  37. !sd.rmdir("dir2/")) {
  38. error("remove/rmdir failed");
  39. }
  40. }
  41. // create a file and write one line to the file
  42. SdFile file("Name1.txt", O_WRITE | O_CREAT);
  43. if (!file.isOpen()) {
  44. error("Name1.txt");
  45. }
  46. file.println("A test line for Name1.txt");
  47. // rename the file name2.txt and add a line.
  48. // sd.vwd() is the volume working directory, root.
  49. if (!file.rename(sd.vwd(), "name2.txt")) {
  50. error("name2.txt");
  51. }
  52. file.println("A test line for name2.txt");
  53. // list files
  54. cout << F("------") << endl;
  55. sd.ls(LS_R);
  56. // make a new directory - "Dir1"
  57. if (!sd.mkdir("Dir1")) {
  58. error("Dir1");
  59. }
  60. // move file into Dir1, rename it NAME3.txt and add a line
  61. if (!file.rename(sd.vwd(), "Dir1/NAME3.txt")) {
  62. error("NAME3.txt");
  63. }
  64. file.println("A line for Dir1/NAME3.txt");
  65. // list files
  66. cout << F("------") << endl;
  67. sd.ls(LS_R);
  68. // make directory "dir2"
  69. if (!sd.mkdir("dir2")) {
  70. error("dir2");
  71. }
  72. // close file before rename(oldPath, newPath)
  73. file.close();
  74. // move Dir1 into dir2 and rename it DIR3
  75. if (!sd.rename("Dir1", "dir2/DIR3")) {
  76. error("dir2/DIR3");
  77. }
  78. // open file for append in new location and add a line
  79. if (!file.open("dir2/DIR3/NAME3.txt", O_WRITE | O_APPEND)) {
  80. error("dir2/DIR3/NAME3.txt");
  81. }
  82. file.println("A line for dir2/DIR3/NAME3.txt");
  83. file.close();
  84. // list files
  85. cout << F("------") << endl;
  86. sd.ls(LS_R);
  87. cout << F("Done") << endl;
  88. }
  89. void loop() {}