Auto update filename

Hi all! I'm working on a simple standalone datalogger for a programable ECU.

My project is almost done except for one thing: the filename. How can I add a simple index to my filename? See, if I make a datalog now, with the name datalog1.log, the next time that file will be rewritten, so I'm looking to add an auto-updateable index, then if I make a new datalog the filename will be datalog2.log, then datalog3.log. I think I need to check my last file in the SD card and add one more to the filename.

How can I do this?

I use date as filename. This is changed at midnight. I guess you could adapt it in may ways to what you want, even to stamp down to the second whenever you start logging.

// This is 17 396

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

RTC_DS1307 RTC;
char filename[] = "00000000.CSV";
File myFile;

void setup()
{
Serial.begin(9600);
Wire.begin(); //Important for RTClib.h
RTC.begin();

if (! RTC.isrunning()) {

Serial.println("RTC is NOT running!");
return;
}

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

pinMode(10, OUTPUT);

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

void loop()
{

getFileName();
createFileName();
delay(3000);
}

void getFileName(){

DateTime now = RTC.now();

filename[0] = (now.year()/1000)%10 + '0'; //To get 1st digit from year()
filename[1] = (now.year()/100)%10 + '0'; //To get 2nd digit from year()
filename[2] = (now.year()/10)%10 + '0'; //To get 3rd digit from year()
filename[3] = now.year()%10 + '0'; //To get 4th digit from year()
filename[4] = now.month()/10 + '0'; //To get 1st digit from month()
filename[5] = now.month()%10 + '0'; //To get 2nd digit from month()
filename[6] = now.day()/10 + '0'; //To get 1st digit from day()
filename[7] = now.day()%10 + '0'; //To get 2nd digit from day()
}

void createFileName(){
myFile = SD.open(filename, FILE_WRITE);
myFile.close();
}

I once made a very simple logger, where I used a file called numname to holde the last file number used.
Everytime the logger was started this file would be increased by 1.
I the used sprintf() to make the new filename.
The names would be:

sens1.txt
sens2.txt and so on

file.open(root, numname,O_READ); //Read the number in the file
char num=file.read();
Serial.println(num); //just for debugging
file.close();

num++;

file.open(root, numname,O_WRITE); //save new number
file.print(num);
file.close();

Serial.println("Number:");  //just for debugging
Serial.println(num);
//Create new filename and file
num=num-'0';
sprintf(newfile, "sens%02d.txt", num);