Removing file from SD card with SD.remove

Dear readers,

I started using Arduino and Arduino IDE approximately a week ago. For one of my larger programs I am writing, I want to make separate a code which can make a logData sheet on a Micro SD card attached via the Micro SD module. This separate code makes the .txt file and gives all the columns a title. For training purposes I am trying to use more of the SD library functionalities.

One of these functions is the SD.remove function. I added it to my code and the first time I ran my code it deleted my file. On consecutive runs it does not anymore. If I look at the Example>SD>File code it looks exactly the same as in there. The example code works, mine does not.

Now what is happening is that I delete the file, check if it exists, which it does and then the column titles are added again. I tried multiple things and now I have around 30 times my column titles in the file :stuck_out_tongue:

I am using a Arduino Uno Rev.3 and also attached my .ino file to this post. If anything is missing please tell me and I will add it to the post. If anything is clear, I'll explain in another way.

Looking forward to the replies.

Sincerely,
Loekatoni

EDIT 1:
The code did not attach as I expected. I would like to add as seen in different posts, but I do not know how yet. For now I'll just copy and past into this area..

EDIT 2: I learned the proper way to post code..

// Global declarations
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // begin serial communication
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // Setting up Mirco SD module
  Serial.println("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("Initialization done.");

  Serial.println("Removing logData.txt");
  SD.remove("logData.txt");
  
  if (SD.exists("logData.txt")) {
    Serial.println("logData.txt exists.");
  } else {
    Serial.println("logData.txt doesn't exist.");
  }
  
  myFile = SD.open("logData.txt", FILE_WRITE);
  if (myFile) { // if the file opened oke
    Serial.println("Writing column titles to logData.csv.");
    myFile.println("Time[-] Temperature[C] Humidity[%]");
    Serial.println("Done writing column titles to logData.csv.");
  } else { // If filed didn't open properly
    Serial.println("Error opening logData.txt.");
  }
  myFile.close();

  myFile = SD.open("logData.txt");
  if (myFile) {
    Serial.println("logData.txt:");

    while (myFile.available()) {
      Serial.write(myFile.read());
    }
  }

  myFile.close();
}

void loop() {
  // Nothing needs to happen to initialize the logData.txt file.
} // end of loop

E1.initializeSdFile.ino (1.41 KB)

loekatoni:
One of these functions is the SD.remove function. I added it to my code and the first time I ran my code it deleted my file. On consecutive runs it does not anymore. If I look at the Example>SD>File code it looks exactly the same as in there. The example code works, mine does not.

Now what is happening is that I delete the file, check if it exists, which it does and then the column titles are added again. I tried multiple things and now I have around 30 times my column titles in the file :stuck_out_tongue:

if (SD.exists("logData.txt")) {

Serial.println("logData.txt exists.");
} else {
Serial.println("logData.txt doesn't exist.");
}

/*
myFile = SD.open("logData.txt", FILE_WRITE);
if (myFile) { // if the file opened oke
Serial.println("Writing column titles to logData.csv.");
myFile.println("Time[-] Temperature[C] Humidity[%]");
Serial.println("Done writing column titles to logData.csv.");
} else { // If filed didn't open properly
Serial.println("Error opening logData.txt.");
}
myFile.close();

myFile = SD.open("logData.txt");
if (myFile) {
Serial.println("logData.txt:");

while (myFile.available()) {
Serial.write(myFile.read());
}
}

myFile.close();*/
}

void loop() {
// Nothing needs to happen to initialize the logData.txt file.
} // end of loop

You seem to have commented out all of the code that would create the file and put headers in it. I don't know how you would end up with 30 copies of the headers.
What is your Serial Monitor output saying?

Dear JohnWasser,

Thank you for responding

In the mean time I have been trying to write the time, temperature and humidity to the file via my main code. For the time I do not have my DS1307 RTC module yet, so for now this is depicted by an x. This has been succesfull, so FYI that you know where those values come from.

I have also deactivated some of the code I posted before. Namely the parts that give titles to the columns. Below is the code with the deactivated lines:

// Global declarations
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // begin serial communication
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // Setting up Mirco SD module
  Serial.println("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("Initialization done.");

  Serial.println("Removing logData.txt");
  SD.remove("logData.txt");
  
  if (SD.exists("logData.txt")) {
    Serial.println("logData.txt exists.");
  } else {
    Serial.println("logData.txt doesn't exist.");
  }
  
  /*
  myFile = SD.open("logData.txt", FILE_WRITE);
  if (myFile) { // if the file opened oke
    Serial.println("Writing column titles to logData.csv.");
    myFile.println("Time[-] Temperature[C] Humidity[%]");
    Serial.println("Done writing column titles to logData.csv.");
  } else { // If filed didn't open properly
    Serial.println("Error opening logData.txt.");
  }
  myFile.close();
*/
  myFile = SD.open("logData.txt");
  if (myFile) {
    Serial.println("logData.txt:");

    while (myFile.available()) {
      Serial.write(myFile.read());
    }
  }

  myFile.close();
}

void loop() {
  // Nothing needs to happen to initialize the logData.txt file.
} // end of loop

And consecutively, below is what my Serial monitor output shows:

Initializing SD card...
Initialization done.
Removing logData.txt
logData.txt exists.
logData.txt:
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
Time[-] Temperature[C] Humidity[%]
x 0 0
x 0 0
x 0 0
x 0 0
x 0 0
x 0 0
x 0 0
x 22 40
x 22 39
x 22 39
x 22 39
x 22 39
x 22 39

Sincerely,
Loekatoni

Edit 1: proper format

Please read the sticky post at the top of the forum and learn how to properly post your code and serial monitor output. It will help others help you. They are called code tags.

The SD.remove() function returns a true/false value. Why are you not checking it? (and printing it?)

Dear blh64,

Thanks for your reply, I corrected the format of my posts.

I did not know about the return of a true/false statement. I will check out what it is, print it and get back to here.

I worry that this section of code is not producing any serial output:

  if (SD.exists("logData.txt")) {
    Serial.println("logData.txt exists.");
  } else {
    Serial.println("logData.txt doesn't exist.");
  }

I can't see any reason for that to happen if the code you show is the code that produces the serial output. Are you sure your new versions of the sketch are uploading without error?

Dear JohnWasser,

I uploaded the wrong Serial monitor output. I updatet the text in the respective post. The code you mention produces:
logData.txt exists, so it is functioning properly and it tells me the file is not deleted.

Dear blh64,

The function I found to return the true/false statement is:

  return SD.remove("logData.txt");

Which I introduce in the complete code as follows.

// Global declarations
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); // begin serial communication
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // Setting up Mirco SD module
  Serial.println("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("Initialization done.");

  Serial.println("Removing logData.txt");
  SD.remove("logData.txt");

  return SD.remove("logData.txt");
  
  if (SD.exists("logData.txt")) {
    Serial.println("logData.txt exists.");
  } else {
    Serial.println("logData.txt doesn't exist.");
  }
  
  /*
  myFile = SD.open("logData.txt", FILE_WRITE);
  if (myFile) { // if the file opened oke
    Serial.println("Writing column titles to logData.csv.");
    myFile.println("Time[-] Temperature[C] Humidity[%]");
    Serial.println("Done writing column titles to logData.csv.");
  } else { // If filed didn't open properly
    Serial.println("Error opening logData.txt.");
  }
  myFile.close();
*/
  myFile = SD.open("logData.txt");
  if (myFile) {
    Serial.println("logData.txt:");

    while (myFile.available()) {
      Serial.write(myFile.read());
    }
  }

  myFile.close();

}

void loop() {
  // Nothing needs to happen to initialize the logData.txt file.
} // end of loop

This does not seem like the function I need. The code "gets stuck" there. It does not continue to consecutive lines. I also don't understand why I want to get this specific statement, as I have put an exist function in the code, which tells me if the file exists or doesn't.

Have you looked at the sd card on a computer? Possibly you have some odd situation where you incorrectly did something previously that has corrupted the file system.

Hi david,

I think this could be the problem indeed. I tried it with a different file name and this seemed to work fine. When I can attach it to a PC, I'll check out if the file is corrupted and maybe delete it from the windows explorer screen.

loekatoni:
Dear blh64,

The function I found to return the true/false statement is:

  return SD.remove("logData.txt");

This is not correct. That function returns a value which you assign to a variable. The return statement exists the current function.

bool result = SD.remove("logData.txt");
Serial.print("The result of trying to remove the file is ");
Serial.println(result? "true" : "false");

Note that you accidentally ADDED a '.remove()' after the existing '.remove()'.

  Serial.println("Removing logData.txt");
  SD.remove("logData.txt");

  return SD.remove("logData.txt");

If the first remove works, the second remove is likely to fail.

Hello repliers,

Took me some time. The initial file was corrupted. Maybe I did not close it was and tried to do some things in the wrong order. I do not know. I deleted all the files and formatted my SD and am now able to use the code on the initial file name again.

Thanks for the help.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.