SD incremental file namer

So I have found many posts that cover how to incrementally name files on an SD card, but I cannot seem to get it to work. Below is what I have been using. I have a tried a few different ways, but all seem to leave me with no way to actually see if the file on the SD card already exists. Currently, what happens is that it always recreates the original file (DATA_000.csv). I am not writing to any special directory, just the root. What I want to do is see if the file exists, and then name a new one incrementally. Any help is appreciated.

Not sure if it makes a difference, but I am doing raw write to the SD.

SdFat       sd;
SdFile      DataFile;
char FileName[13];

void get_FileName(char *FileName)
{
      
      FileName[0] = 'D';
      FileName[1] = 'A';
      FileName[2] = 'T';
      FileName[3] = 'A';      
      FileName[4] = '_';
      FileName[5] = '00';
      FileName[6] = '00';
      FileName[7] = '00';
      FileName[8] = '.';
      FileName[9] = 'c';
      FileName[10] = 's';
      FileName[11] = 'v';
      
      
      for(int i=0;i<100;i++)
      {   
          FileName[5] = (i/100)%10 + '0';  
          FileName[6] = (i/10)%10 + '0'; 
          FileName[7] = i%10 + '0';
          
          Serial.print("Searching for ");
          Serial.println(FileName);
 
          if (!sd.exists(FileName)) 
          {
              Serial.print("Chosen Name: ");
              Serial.println(FileName); 
              return;
          }
          else
          {
              Serial.print(FileName);
              Serial.println(" already exists!");
          }
      }
}

What I want to do is see if the file exists, and then name a new one incrementally.

So, what does the code do? What serial output do you get?

And, where is the rest of the code?

PaulS, here is more of the code. It is just a small chunk from my sketch, but I think I have included all of the necessary elements relating to this problem:

        get_FileName(FileName);
        if (!sd.begin(SPIHardwareSS, spiSpeed))  
        {
            Serial.println("Card Initialization failed");
            NoSDCard = 1;
            OnLED(RED1);
        }
        else
        {
            Serial.println("Card Initialized. ");
            Serial.print("Creating: ");
            Serial.print(FileName);
            Serial.print("...");
        }
        
        if(sd.remove(FileName)) 
        {
            Serial.print("File Already Exists...File Re-created...");
        }
        // Create a contiguous file
        if (!DataFile.createContiguous(sd.vwd(), FileName, 512UL*BLOCK_COUNT) && NoSDCard == 0) 
        {
            OnLED(RED1);
            //error("Unable to Create Contiguous Range in Init()");
            Serial.println("Unable to Create Contiguous Range in SD_Init()");
            NoSDCard = 1;
        }

void get_FileName(char *FileName)
{
      
      FileName[0] = 'D';
      FileName[1] = 'A';
      FileName[2] = 'T';
      FileName[3] = 'A';      
      FileName[4] = '_';
      FileName[5] = '00';
      FileName[6] = '00';
      FileName[7] = '00';
      FileName[8] = '.';
      FileName[9] = 'c';
      FileName[10] = 's';
      FileName[11] = 'v';
      
      
      for(int i=0;i<100;i++)
      {   
          FileName[5] = (i/100)%10 + '0';  
          FileName[6] = (i/10)%10 + '0'; 
          FileName[7] = i%10 + '0';
          
          Serial.print("Searching for ");
          Serial.println(FileName);
 
          if (!sd.exists(FileName)) 
          {
              Serial.print("Chosen Name: ");
              Serial.println(FileName); 
              return;
          }
          else
          {
              Serial.print(FileName);
              Serial.println(" already exists!");
          }
      }
}

when I start to run the code provided, if I have a clean SD card that has just been formatted, the file ends up getting created successfully. When I do not format the card before hand (which means DATA_000.CSV exists prior to starting the code) the code is not accurately determining that DATA_000.csv already exists. The results of the code above are:
“Searching for DATA_000.csv
Chosen Name: DATA_000.csv
Card Initialized.
Creating: DATA_000.csv…File Already Exists…File Re-created…”

When you initialize the file name in get_FileName you don't add a null on the end.

      FileName[12] = 0;

You also access the sd device in get_FileName before you open it with sd.begin.

Pete

Well I feel pretty silly at the moment. Thanks Pete for that bit of guidance. The reason it was not working is because I was trying to access the card before initializing it =). Now it works like a charm!