SD Card: Dynamic file name with a number - DataLOG###.csv

I want a simple count number next to the fileName when I log measurements to the SD like, DataLOG001.csv and each time I turn on the device I want the device to create a new fileName DataLOG002.csv, DataLOG003.csv...

I am very inexperienced and I have tried a lot of things but without luck.

Right now I use this normal code

File dataFile = SD.open("DataLOG.csv", FILE_WRITE);

  Serial.print("Writing to file: " );
  Serial.println("DataLOG.csv");
  dataFile.println("");
  dataFile.println("Count,GAS(ppb),GAS_ADC,Temperature,Humidity,Days,Hours,Minutes,Seconds");
  dataFile.close();

You will find the source code attached.

Then, I tried to implement the code from the examples below into my code but I didn't manage to make it work properly.

SPEC-DGS-NO2-15SEP17-LOGGER_v2.1S.ino (9.4 KB)

It shouldn't be too hard. All you need is to read and re-write a counter in setup, and somewhere to store it. Another file with a fixed name on the same SD might suffice. If you use an RTC, you could use a timestamp as the file name, which is common practice.

I just need a simple count and no time-stamp.

The problem I am facing is that the code at first generates a row of the information (temp/humidity/etc...) and then later writes the measurements to the same file. I can't sync them together if I add a code like the above.

// create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // leave the loop!
    }
  }