File names on SD cards need to adhere to the 8.3 format; yours doesn't.
The below shows how you can generate a filename between 00000000.txt and 99999999.txt. I unfortunately don't have an SD card to complete the code and test.
// the filename
char filename[13];
void setup()
{
Serial.begin(57600);
while(!Serial);
for (uint32_t cnt = 0; cnt <= 99999999; cnt++)
{
sprintf(filename, "%08lu.txt",cnt);
Serial.println(filename);
// test if file exist using https://www.arduino.cc/en/Reference/SDexists
// use break to abort the for-loop once you have found a file that does not exist
}
}
void loop()
{
}
okay, understood. TEXTFILE.TXT should work I assume.
Can second and onwards files be named TEXTFILE_1.txt and so on and so forth? is there a way to do that? Would be easier than the number file naming system.
// the filename
char filename[13];
const char basename[] = "file__";
void setup()
{
// indicates if a new filename was found
bool newfile = false;
Serial.begin(57600);
while (!Serial);
int maxnumber = 1;
for (uint8_t cnt = 0; cnt < 8 - strlen(basename); cnt++)
{
maxnumber *= 10;
}
maxnumber -= 1;
Serial.print("maxnumber = "); Serial.println(maxnumber);
uint8_t maxdigits = 8 - strlen(basename);
Serial.print("maxdigits = "); Serial.println(maxdigits);
char format[15];
snprintf(format, sizeof(format), "%%s%%0%uu.txt", maxdigits);
Serial.print("format string = "); Serial.println(format);
for (uint16_t cnt = 0; cnt <= maxnumber; cnt++)
{
snprintf(filename, sizeof(filename), format, basename, cnt);
Serial.println(filename);
// test if file exist using https://www.arduino.cc/en/Reference/SDexists
// set newfile to true if a non-existing file was found
// use break to abort the for-loop once you have found a file that does not exist
}
if (newfile == false)
{
Serial.println("Could not find an none-existing file");
for (;;);
}
else
{
Serial.print("New file is '");
Serial.print(filename);
Serial.println("'");
}
}
void loop()
{
}
You can basically use anything for the basename.
Personal opinion, but it would be a waste if you want 'unlimted' files
These characters can be whatever letters and digits (and a few other printable chars) you like. File extensions generally describe the type of file, so .txt for a text file, .xls for Excel spreadsheet etc.
You could name your files TEXTFILE.001, TEXTFILE.002 etc. To could also use LOG00001.TXT, LOG00002.TXT etc.
How do you determine that? If it's on a power-on / reset, you can put it in setup().
Now we finally know what you want to call your files, we don't have to go through most of the complicated stuff in my second eample;
// the filename
char filename[13];
void setup()
{
// indicates if a new filename was found
bool newfile = false;
Serial.begin(57600);
while (!Serial);
for (uint16_t cnt = 0; cnt <= 9999; cnt++)
{
snprintf(filename, sizeof(filename), "LOG%04u.txt", cnt);
Serial.println(filename);
// test if file exist using https://www.arduino.cc/en/Reference/SDexists
// set newfile to true if a non-existing file was found
// use break to abort the for-loop once you have found a file that does not exist
}
if (newfile == false)
{
Serial.println("Could not find an non-existing file");
for (;;);
}
else
{
Serial.print("New file is '");
Serial.print(filename);
Serial.println("'");
}
}
void loop()
{
}
The Files example in the SD library has a few lines relating to SD.exists().