#include <SD.h>
#define CS_PIN 10

void createDirectoryAndFile(const char *dirName) {
  // Create a directory
  Serial.print(dirName);
  if (SD.mkdir(dirName)) {
    Serial.println(" Directory created successfully");
  } else {
    Serial.println(" => Error creating directory");
    return;
  }
}

void dumpFile(const char * path) {
  File file = SD.open(path, FILE_READ);
  if (file) {
    Serial.print("\n\nContent of: "); Serial.println(path);
    Serial.println("----------------------");
    while (file.available()) Serial.write(file.read());
    Serial.println("----------------------");
    file.close();
  } else {
    Serial.print("error reading "); Serial.println(path);
  }
}

void setup() {
  Serial.begin(115200);

  Serial.print("Initializing SD card... ");

  if (!SD.begin(CS_PIN)) {
    Serial.println("Card initialization failed!");
    while (true);
  }
  Serial.println("initialization done.");

  createDirectoryAndFile("DIR1");
  createDirectoryAndFile("DIR2");

  File file = SD.open("DIR1/log1.txt", FILE_WRITE);
  if (file) {
    file.println("This content is in file DIR1/log1.txt");
    file.close();
    Serial.println("Done writing in DIR1/log1.txt.");
  } else {
    Serial.println("error creating DIR1/log1.txt.");
  }

  file = SD.open("DIR2/log1.txt", FILE_WRITE);
  if (file) {
    file.println("This content is in file DIR2/log1.txt");
    file.close();
    Serial.println("Done writing in DIR2/log1.txt.");
  } else {
    Serial.println("error creating DIR2/log1.txt.");
  }

  dumpFile("DIR1/log1.txt");
  dumpFile("DIR2/log1.txt");
}


void loop() {}