# SD.open(filepath) Variable Filepath

**URL:** <https://forum.arduino.cc/t/sd-open-filepath-variable-filepath/1210288>\
**Category:** General Guidance\
**Created:** [January 12, 2024, 6:29pm UTC](https://forum.arduino.cc/t/sd-open-filepath-variable-filepath/1210288 "2024-01-12T18:29:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![iangill](https://avatars.discourse-cdn.com/v4/letter/i/ccd318/32.png) [@iangill](https://forum.arduino.cc/u/iangill)\
**Post date:** [January 12, 2024, 6:29pm UTC](https://forum.arduino.cc/t/sd-open-filepath-variable-filepath/1210288/1 "2024-01-12T18:29:06Z")

</div>

I’m trying to write a sketch which logs humidity over time and records it to an SD card. It works fine with a fixed file name but I want it to write each record to a different file. I’ve used this to generate a new file name each time as a char[] string.

```arduino
  dt = clock.getDateTime();
  sprintf(name, "%4d%02d%02d%02d%02d.csv", dt.year, dt.month, dt.day, dt.hour, dt.minute);
  Serial.println(name);

```

I then use  
`myFile = SD.open(name, FILE_WRITE);`

to create the file.  
When I run the sketch It doesn’t actually create the file and prints “error opening 202401121822.csv”

Is it a problem using a string to create a file name?

The full sketch is below.

```arduino
#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
#include <SD.h>
#include <Wire.h>
#include <DS3231.h>

DS3231 clock;
RTCDateTime dt;
static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
int humid;
char name[18];
File myFile;

/*
 * Initialize the serial port.
 */
void setup() {
  Serial.begin(9600);
  /*while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  */

  for (int x = 0; x < 10; x++) {
    Serial.println();
  }
  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1)
      ;
  }
  Serial.println("initialization done.");

  Serial.println("Initialize DS3231");
  // Initialize DS3231
  clock.begin();

  dt = clock.getDateTime();
  sprintf(name, "%4d%02d%02d%02d%02d.csv", dt.year, dt.month, dt.day, dt.hour, dt.minute);
  Serial.println(name);
  

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
}

/*
 * Poll for a measurement, keeping the state machine alive. Returns
 * true if a measurement is available.
*/

static bool measure_environment(float *temperature, float *humidity) {
  static unsigned long measurement_timestamp = millis();

  if (millis() - measurement_timestamp > 5000ul) {
    if (dht_sensor.measure(temperature, humidity) == true) {
      measurement_timestamp = millis();
      return (true);
    }
  }

  return (false);
}

/*
 * Main program loop.
 */
void loop() {
  float temperature;
  float humidity;
  char data[10];

  /* Measure temperature and humidity. If the functions returns
     true, then a measurement is available. */
  if (measure_environment(&temperature, &humidity) == true) {

    if (humidity != humid) {

      digitalWrite(LED_BUILTIN, HIGH);

      Serial.print(dt.hour);
      Serial.print(":");
      Serial.print(dt.minute);
      Serial.print(":");
      Serial.print(dt.second);
      Serial.print("\t");

      Serial.print(" ");
      Serial.print("T = ");
      Serial.print(temperature, 1);
      Serial.print(" deg. C, H = ");
      Serial.print(humidity, 1);
      Serial.println("%");

      myFile = SD.open(name, FILE_WRITE);

      if (myFile) {
        myFile.print(dt.year);
        myFile.print(",");
        myFile.print(dt.month);
        myFile.print(",");
        myFile.print(dt.day);
        myFile.print(",");
        myFile.print(",");
        myFile.print(dt.hour);
        myFile.print(",");
        myFile.print(dt.minute);
        myFile.print(",");
        myFile.print(dt.second);
        myFile.print(",");
        myFile.print(",");
        myFile.print(temperature);
        myFile.print(",");
        myFile.println(humidity);
        // close the file:
        myFile.close();

      } else {
        // if the file didn't open, print an error:
        Serial.print("error opening "); Serial.println(name);
      }
      digitalWrite(LED_BUILTIN, LOW);
      humid = humidity;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [January 12, 2024, 6:35pm UTC](https://forum.arduino.cc/t/sd-open-filepath-variable-filepath/1210288/2 "2024-01-12T18:35:09Z")

</div>

> [@iangill](#):
>
> Is it a problem using a string to create a file name?

No, but that string is too long. At most eight characters are allowed before the '.csv'. The file name format is 8.3.

---

<div class="post-metadata">

**Author:** ![iangill](https://avatars.discourse-cdn.com/v4/letter/i/ccd318/32.png) [@iangill](https://forum.arduino.cc/u/iangill)\
**Post date:** [January 12, 2024, 6:54pm UTC](https://forum.arduino.cc/t/sd-open-filepath-variable-filepath/1210288/3 "2024-01-12T18:54:42Z")

</div>

You're a star. I just took the year out of the file name and it works fine.

Thanks very much.

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [January 12, 2024, 7:28pm UTC](https://forum.arduino.cc/t/sd-open-filepath-variable-filepath/1210288/4 "2024-01-12T19:28:58Z")

</div>

Just out of curiosity, why would you want to create hundreds (thousands?) of files, each containing a single line?

---

<div class="post-metadata">

**Author:** ![iangill](https://avatars.discourse-cdn.com/v4/letter/i/ccd318/32.png) [@iangill](https://forum.arduino.cc/u/iangill)\
**Post date:** [January 12, 2024, 11:38pm UTC](https://forum.arduino.cc/t/sd-open-filepath-variable-filepath/1210288/5 "2024-01-12T23:38:55Z")

</div>

I don't want to create thousands of files. I just want all the files that i create to be indexed. If they are all sorted by name, they will be in date order.

---

<div class="post-metadata">

**Author:** ![iangill](https://avatars.discourse-cdn.com/v4/letter/i/ccd318/32.png) [@iangill](https://forum.arduino.cc/u/iangill)\
**Post date:** [January 12, 2024, 11:41pm UTC](https://forum.arduino.cc/t/sd-open-filepath-variable-filepath/1210288/6 "2024-01-12T23:41:10Z")

</div>

Each file wont contain a single line, each file may contain hundreds.  
Just a new file name (by date and time) for each entry.

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [January 13, 2024, 3:42am UTC](https://forum.arduino.cc/t/sd-open-filepath-variable-filepath/1210288/7 "2024-01-13T03:42:21Z")

</div>

Your code takes a reading every 5 seconds. You use seconds in the name of your file. You will only have 1 entry in the filename and the next time you sample, it will be in a different file.

---

<div class="post-metadata">

**Author:** ![iangill](https://avatars.discourse-cdn.com/v4/letter/i/ccd318/32.png) [@iangill](https://forum.arduino.cc/u/iangill)\
**Post date:** [January 13, 2024, 9:19am UTC](https://forum.arduino.cc/t/sd-open-filepath-variable-filepath/1210288/8 "2024-01-13T09:19:17Z")

</div>

The file name is generated in the setup. After that it just keeps writing to the same file name.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 11, 2024, 9:20am UTC](https://forum.arduino.cc/t/sd-open-filepath-variable-filepath/1210288/9 "2024-07-11T09:20:13Z")

</div>

Closed on July 11, 2024, 9:20am UTC

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