Write to a file on an SD card inside a folder?

Hello everyone.

I am trying to store files inside a series of folders, depending on the date.

I am using a RTC, and the standard SD library.

I have previously gotten the program to set the file name to the date, and I can figure out how to make my folders with the date, but I can not seem to figure out how to store my data logger file inside said folder.

Anyone have any suggestions?

Thank you.

#include <stdarg.h>
#include <Wire.h>
#include "RTClib.h"
#include <SD.h>


File root;
RTC_DS1307 RTC;
void setup()
{
  //RTC.adjust(DateTime(__DATE__, __TIME__));    //sets RTC time to PC time at compiling.
  Serial.begin(9600);
  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin 
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
  // or the SD library functions will not work. 
  pinMode(10, OUTPUT);

  if (!SD.begin(8)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");


  File logfile;

  Wire.begin();
  RTC.begin();

  Serial.println("done!");
  DateTime now = RTC.now();

  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();

  // create a new file
  char foldername[] = "03/03";
  foldername[0] = now.month()/10 + '0'; //To get 1st digit from month()
  foldername[1] = now.month()%10 + '0'; //To get 2nd digit from month()
  foldername[3] = now.day()/10 + '0'; //To get 1st digit from day()
  foldername[4] = now.day()%10 + '0'; //To get 2nd digit from day()

  char filename[] = "0003AGM.CSV";
  filename[0] = now.hour()/10 + '0'; //To get 1st digit from hour()
  filename[1] = now.hour()%10 + '0'; //To get 2nd digit from hour()
  filename[2] = now.minute()/10 + '0'; //To get 1st digit from minute()
  filename[3] = now.minute()%10 + '0'; //To get 2nd digit from minute()

  Serial.println(foldername);
  Serial.println(filename);

  SD.mkdir(foldername);
  if (! SD.exists(filename)) {
    // only open a new file if it doesn't exist
    logfile = SD.open("foldername/filename", FILE_WRITE);
    logfile.print("test");
    logfile.flush();
  }

It's not an SD problem, it's a string problem. You are trying to open a file named "foldername/filename". This won't work since there is no folder named "foldername". In fact what you want to do is produce a string containing the value of the variable foldername, plus the file separator (/) plus the value of the variable filename. You can use sprintf to concatenate these strings into a single string that you can use as your file name. (You can also use sprintf to tidy up your code to convert the date into a folder name.) There are lots of other ways to concaqtenate strings, but I'd say that sprintf is the simplest and most powerful solution for what you're trying to do.

Hello,

I am trying to do something similar to what the creator of this post tried. This is the most relevant post even though it is a few years old, it did come up in a google search so to help others that search for this in the future I decided best to not create a new thread.

I would like to ask clarity is there a way to create a folder using the arduino and a sd card?
The way I am reading this post, PeterH suggested to create a string, and use the variable of the filename, and combine it with the variable of the foldername, in what i'm assuming would be fairly easy to parse from one large "masterfile"

But I would like actual files inside a folder so that I can open each file using my computer and see only the data of the file, rather than having a "masterfile" with everything on that one large file.

Is it possible to create a folder, and a file within a folder the way I am envisioning? Or is parsing data from one large "masterfile" the only way to accomplish this?

Thomas