Go Down

Topic: Write to a file on an SD card inside a folder? (Read 980 times) previous topic - next topic


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.
Code: [Select]
#include <stdarg.h>
#include <Wire.h>
#include "RTClib.h"
#include <SD.h>

File root;
void setup()
  //RTC.adjust(DateTime(__DATE__, __TIME__));    //sets RTC time to PC time at compiling.
  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!");
  Serial.println("initialization done.");

  File logfile;


  DateTime now = RTC.now();

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

  // 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()


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


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.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131