How to use the SD folder's name here?

Hi all.
I'd like to operate the SD with subfolder, if set: String stringA = "Friday"; write a file into subfolder 'Friday', failed, how to fix please?

tested: 'writeFile(SD, "/Friday/Mondayhello.txt", "mmHello ");` works fine.

Thanks
Adam


#include "FS.h"
#include "SD.h"
#include "SPI.h"

String stringA = "Friday";
String stringB = "Wednesday";
String stringC;

String StrWK[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

int writeFileButtonmark = 1;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  SDsetup();

  if (writeFileButtonmark == 1) {

    for (int i = 0; i < 7; i++) {
      if (stringA == StrWK[i]) {
        Serial.print("L36strArray["); Serial.print( i); Serial.print( "] =");
        Serial.println(StrWK[i]);

        writeFile(SD, "/StrWK[i]/Mondayhello.txt", "mmHello ");
        appendFile(SD, "/StrWK[i]/Mondayhello.txt", "mmWorld!\n");
        readFile(SD, "/StrWK[i]/Mondayhello.txt");
        writeFileButtonmark = 0;
        delay(1000);
      }
    }
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}


void readFile(fs::FS & fs, const char * path) {
  Serial.printf("Reading file: %s\n", path);

  File file = fs.open(path);
  if (!file) {
    Serial.println("Failed to open file for reading");
    return;
  }

  Serial.print("Read from file: ");
  while (file.available()) {
    Serial.write(file.read());
  }
  file.close();
}

void writeFile(fs::FS & fs, const char * path, const char * message) {
  Serial.printf("Writing file: %s\n", path);

  File file = fs.open(path, FILE_WRITE);
  if (!file) {
    Serial.println("Failed to open file for writing");
    return;
  }
  if (file.print(message)) {
    Serial.println("File written");
  } else {
    Serial.println("Write failed");
  }
  file.close();
}

void appendFile(fs::FS & fs, const char * path, const char * message) {
  Serial.printf("Appending to file: %s\n", path);

  File file = fs.open(path, FILE_APPEND);
  if (!file) {
    Serial.println("Failed to open file for appending");
    return;
  }
  if (file.print(message)) {
    Serial.println("Message appended");
  } else {
    Serial.println("Append failed");
  }
  file.close();
}


void SDsetup() {
  Serial.begin(115200);
  if (!SD.begin()) {
    Serial.println("Card Mount Failed");
    return;
  }
  uint8_t cardType = SD.cardType();

  if (cardType == CARD_NONE) {
    Serial.println("No SD card attached");
    return;
  }

  Serial.print("SD Card Type: ");
  if (cardType == CARD_MMC) {
    Serial.println("MMC");
  } else if (cardType == CARD_SD) {
    Serial.println("SDSC");
  } else if (cardType == CARD_SDHC) {
    Serial.println("SDHC");
  } else {
    Serial.println("UNKNOWN");
  }

  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);

}

OUTPUT:

D Card Type: SDHC
SD Card Size: 29818MB
L36strArray[4] =Friday
Writing file: /StrWK[i]/Mondayhello.txt
Failed to open file for writing
Appending to file: /StrWK[i]/Mondayhello.txt
Failed to open file for appending
Reading file: /StrWK[i]/Mondayhello.txt
Failed to open file for reading

You need to investigate the sprintf function.

And no doubt someone will be along shortly to tell you not to use Strings, for perfectly valid reasons.

1 Like

Thank you.
It works.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.