How to Retain Old File and Create more files in SD card?

Hi Guy, I have a question about SD Card.

I want to know how to code this scenerio.

You have a old file in the SD card but you don't want it to be deleted or be removed, you don't want it also to be over write. I want to happen is that when there is a old file i want that i can save again a new file without affecting the old data.

That can make it create more files, such as datalog1.txt, datalog2.txt... not deleting the old one and replace it with new.

#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


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

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

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}

Does anybody know, what to code to use and where to put.
I don't want to use SD.remove() i want to retain the old data and make a new data like test.txt, test1.txt, test2.txt and so on, if we will gather a new data.

You seem to be trying to make a simple problem difficult. The old file will not be deleted unless you specifically delete it. I don't know if it is possible to overwrite a file - I suspect it isn't, and it's certainly not something to be worried about.

If you want a new file you simply nominate a new filename and write to it. A common way to do this is to use the date as a filename and create a new one at midnight.

I don't know if it is possible to overwrite a file - I suspect it isn't

It most certainly is. Open an existing file, write some data, close the file, and say goodbye to the old data.

OP:
You need to loop through a set of values, creating a file name. See if the file exists. If it does, do not open and write to that file. If it doesn't, then you can open and write to that file.

That's funny. I open an existing file, write a line of data, close the file, and that data is simply added as a new line to the data that is already there.

void loop() {
.
.
    WriteSD();
.
. 
}

void WriteSD()
{  
           myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  myFile.print(hour);
  myFile.print(":");
  myFile.print(minute);
  myFile.print(":");
  myFile.print(second);
  myFile.print(",");

  myFile.print(InTemp);
  myFile.print(",");
  myFile.print(OutTemp);
  myFile.print(",");
  myFile.print(DrainTemp);
  myFile.print(",");
  myFile.println(ShrTemp);
       myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE     
}

That's funny. I open an existing file, write a line of data, close the file, and that data is simply added as a new line to the data that is already there.

That depends on how you open a file for write. Unlike every other system I've ever used, the SD class defaults to append.

So, OP, in the light of your need

rajhefondo:
don't want it to be deleted

and your code

#include <SD.h>

you can safely ignore reply #2, and you don't have to say goodbye to anything.

The code you are using is a standard one-shot write and read back test, but which actually deprives you of a test of a real life situation like the data logging propose. In practice, a lot of that stuff is superfluous. You just need a minimal test of the card in the setup, and a myFile.print sequence in the loop, rather like that shown in reply #3.

I'm confuse now, sorry I am bad in understanding english and speaking in english what I want to say.

I just want the file to increment every time we record a new data without removing the old data and i dont know how to increment it in my code

Read reply #6 again....... twice.

All you need do is have the myFile.print statements in the loop. As you go round the loop, you read and/or derive data and you then print it to SD. That is all there is to it. You will not lose any data, nothing will be removed.