SD logging to file 1 only

Hi, I’m having issues with getting the SD card to record data to multiple files. Below, the code is able to create a new file each time the SD is inserted, and the correct headings are logged to each file, but all of the data is logged to file one. No matter how many files are created with headings, the data keeps being logged to the first file. I was wondering is anyone could figure out why this is happening.

File dataFile;



 void setup() {               
 pinMode(SS, OUTPUT);
 
  if (SD.begin(10,11,12,13)) {          

  String fileName = String();
  String message = String();
  unsigned int filenumber = 1;
  while(!filenumber==0) {
    fileName = "file_";
    fileName += filenumber;
    fileName += ".csv";
    message = fileName;
    char charFileName[fileName.length() + 1];
    fileName.toCharArray(charFileName, sizeof(charFileName));

    if (SD.exists(charFileName)) { 
      message += " exists.";
      filenumber++;
    }
    else {
      File dataFile = SD.open(charFileName, FILE_WRITE);
      message += " created.";
      dataFile.println("Date, Time");
      dataFile.close();
      filenumber = 0;
   }
    Serial.println(message);
  }


  }

 }



void loop() {



  float tempReading;

  
   String fileName = String();                                  //at the moment, here
   String message = String();
   unsigned int filenumber = 1; 
    fileName = "file_";
    fileName += filenumber;                                        //
    fileName += ".csv";
    message = fileName;
    char charFileName[fileName.length() + 1];                   
    fileName.toCharArray(charFileName, sizeof(charFileName));         //to here defines file number one, so all data is currently logged to first file. needs to log to most recent file.

    
  static unsigned long lastTime = 0;
  const long interval = 10000;            //every ten seconds, record values
  static bool state = 0;



  unsigned long now = millis();
if ( now - lastTime > interval && state == 0 /*&& SD.open(charFileName, FILE_WRITE)*/ ) {
    state = 0;
    lastTime = now;
    DateTime now = rtc.now();

    File dataFile = SD.open(charFileName, FILE_WRITE); 
    
    dataFile.print("\n");
    dataFile.print(daysOfTheWeek[now.dayOfTheWeek()]);
    dataFile.print("/");
    dataFile.print(now.day(), DEC);
    dataFile.print("/");
    dataFile.print(now.month(), DEC);
    dataFile.print("/");
    dataFile.print(now.year(), DEC);
    dataFile.print(",");
    dataFile.close();
                           

        
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print("/");
    Serial.print(now.day(), DEC);
    Serial.print("/");
    Serial.print(now.month(), DEC);
    Serial.print("/");
    Serial.print(now.year(), DEC);
    Serial.print(",");
    Serial.print("\n"); 
  
}
  }

I’ve removed some of the variables that are usually recorded, such as voltage, to make the sample of my code shorter. I also do not have permission to post the entire code so hopefully the problem can be solved from this small sample, although I know that not having the full code can make things difficult.
If anyone has any ideas let me know,
Thanks