選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

107 行
2.5KB

  1. /*
  2. * This program demonstrates use of SdFile::rename()
  3. * and SdFat::rename().
  4. */
  5. #include <SPI.h>
  6. #include "SdFat.h"
  7. #include "sdios.h"
  8. // SD chip select pin
  9. const uint8_t chipSelect = SS;
  10. // file system
  11. SdFat sd;
  12. // Serial print stream
  13. ArduinoOutStream cout(Serial);
  14. //------------------------------------------------------------------------------
  15. // store error strings in flash to save RAM
  16. #define error(s) sd.errorHalt(F(s))
  17. //------------------------------------------------------------------------------
  18. void setup() {
  19. Serial.begin(9600);
  20. // Wait for USB Serial
  21. while (!Serial) {
  22. SysCall::yield();
  23. }
  24. cout << F("Insert an empty SD. Type any character to start.") << endl;
  25. while (!Serial.available()) {
  26. SysCall::yield();
  27. }
  28. // Initialize at the highest speed supported by the board that is
  29. // not over 50 MHz. Try a lower speed if SPI errors occur.
  30. if (!sd.begin(chipSelect, SD_SCK_MHZ(50))) {
  31. sd.initErrorHalt();
  32. }
  33. // Remove file/dirs from previous run.
  34. if (sd.exists("dir2/DIR3/NAME3.txt")) {
  35. cout << F("Removing /dir2/DIR3/NAME3.txt") << endl;
  36. if (!sd.remove("dir2/DIR3/NAME3.txt") ||
  37. !sd.rmdir("dir2/DIR3/") ||
  38. !sd.rmdir("dir2/")) {
  39. error("remove/rmdir failed");
  40. }
  41. }
  42. // create a file and write one line to the file
  43. SdFile file("Name1.txt", O_WRONLY | O_CREAT);
  44. if (!file.isOpen()) {
  45. error("Name1.txt");
  46. }
  47. file.println("A test line for Name1.txt");
  48. // rename the file name2.txt and add a line.
  49. if (!file.rename("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("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_WRONLY | 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() {}