Remove file with "SdFat.h"

Hi,
i need to remove files from Sd card after i red it.
I'm using SdFat.h.
This is "ReadWriteSdFat" example with the last one code line added by me:

// Ported to SdFat from the native Arduino SD library example by Bill Greiman
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const int chipSelect = 4;
/*
 SD card read/write
  
 This example shows how to read and write data to and from an SD card file 	
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created   Nov 2010
 by David A. Mellis
 updated 2 Dec 2010
 by Tom Igoe
 modified by Bill Greiman 11 Apr 2011
 This example code is in the public domain.
 	 
 */
#include <SdFat.h>
SdFat sd;
SdFile myFile;

void setup() {
  Serial.begin(9600);
  while (!Serial) {}  // wait for Leonardo
  Serial.println("Type any character to start");
  while (Serial.read() <= 0) {}
  delay(400);  // catch Due reset problem
  
  // Initialize SdFat or print a detailed error message and halt
  // Use half speed like the native library.
  // change to SPI_FULL_SPEED for more performance.
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

  // open the file for write at end like the Native SD library
  if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening test.txt for write failed");
  }
  // if the file opened okay, write to it:
  Serial.print("Writing to test.txt...");
  myFile.println("testing 1, 2, 3.");

  // close the file:
  myFile.close();
  Serial.println("done.");

  // re-open the file for reading:
  if (!myFile.open("test.txt", O_READ)) {
    sd.errorHalt("opening test.txt for read failed");
  }
  Serial.println("test.txt:");

  // read from the file until there's nothing else in it:
  int data;
  while ((data = myFile.read()) >= 0) Serial.write(data);
  // close the file:
  if (!myFile.close()) Serial.println("Error file.close");
  if (!myFile.remove()) Serial.println("Error file.remove");
}

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

But i get "file remove" error.

You must not close the file before calling remove.

Just use remove instead of close like this:

 // read from the file until there's nothing else in it:
  int data;
  while ((data = myFile.read()) >= 0) Serial.write(data);
  // remove the file:
  if (!myFile.remove()) Serial.println("Error file.remove");

Now it works.
Thank you.

Hi fat16lib,

I wonder if you can help me please?

  SdFile delfile;
  if (!delfile.open(infile, O_READ)) {
    sd.errorHalt("opening infile for read failed");
  }
  if (!delfile.remove()) {
    sd.errorHalt("deleting infile failed");
  }

Syntaxicly, this is apparently correct, but I always get deleting infile failed.... the only thing I can think is that the reason is because:-

 infile = "ROUTE/XXXXXXXX.CSV";

Could you confirm if this is the reason please? If so, how would I achieve the result I am after?

EDIT: That is NOT the problem.....I put the source files in 'root' same result.... Baffled now...

Regards,

Graham

I think the problem is that the file is opened read only.

  if (!delfile.open(infile, O_READ)) {

Try open with write.

  if (!delfile.open(infile, O_READ | O_WRITE)) {

You can just call

 sd.remove(infile);

fat16lib:
You can just call

 sd.remove(infile);

Perfect, thankyou.

Graham

Hi guys,

I'm using a Arduino DUE with a CTE TFT shield, which has a SD slot.
I tried the code above, with the modification proposed by fat16lib, just calling myFile.remove() at the end.
I also changed the value of chipSelect to 53, since I'm using a DUE.

// Ported to SdFat from the native Arduino SD library example by Bill Greiman
// On the Ethernet Shield, CS is pin 4. SdFat handles setting SS
const uint8_t chipSelect = 53;
/*
 SD card read/write
  
 This example shows how to read and write data to and from an SD card file 	
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 created   Nov 2010
 by David A. Mellis
 updated 2 Dec 2010
 by Tom Igoe
 modified by Bill Greiman 11 Apr 2011
 This example code is in the public domain.
 	 
 */
#include <SdFat.h>
SdFat sd;
SdFile myFile;

void setup() {
  Serial.begin(9600);
  while (!Serial) {}  // wait for Leonardo
  Serial.println("Type any character to start");
  while (Serial.read() <= 0) {}
  delay(400);  // catch Due reset problem
  
  // Initialize SdFat or print a detailed error message and halt
  // Use half speed like the native library.
  // change to SPI_FULL_SPEED for more performance.
  if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

  // open the file for write at end like the Native SD library
  if (!myFile.open("test.txt", O_RDWR | O_CREAT | O_AT_END)) {
    sd.errorHalt("opening test.txt for write failed");
  }
  // if the file opened okay, write to it:
  Serial.print("Writing to test.txt...");
  myFile.println("testing 1, 2, 3.");

  // close the file:
  myFile.close();
  Serial.println("done.");

  // re-open the file for reading:
  if (!myFile.open("test.txt", O_READ)) {
    sd.errorHalt("opening test.txt for read failed");
  }
  Serial.println("test.txt:");

 // read from the file until there's nothing else in it:
  int data;
  while ((data = myFile.read()) >= 0) {
    Serial.write(data);
  }
  // remove the file:
  if (!myFile.remove()) Serial.println("Error file.remove");
}


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

What happens is that everything goes well, except for removing the file.
Continuous runs just append the data.

Here's the output:

Type any character to start
Writing to test.txt...done.
test.txt:
testing 1, 2, 3.
testing 1, 2, 3.
testing 1, 2, 3.
testing 1, 2, 3.
testing 1, 2, 3.
testing 1, 2, 3.
testing 1, 2, 3.
testing 1, 2, 3.
testing 1, 2, 3.
Error file.remove

Any ideas? Apparently the connection to SD card is fine, since it can read and write.
Edit:
tried to get the newest version of the library, but cannot see any difference. Still doesn't work. Took a look at the cpp code, but can't really determine where it goes wrong... truncating or something else...
Any ideas how to debug this?

Edit 2:
Solved it, by replacing

myFile.remove();

with

sd.remove("test.txt")

Still would like to know how this can be caused, feels like a workaround and I can't imagine this should be regarded a bug in the library. I also realize this solution is also mentioned in this thread, didn't think it applied on my case :zipper_mouth_face:

You can't remove a file that has been opened read only.

 if (!myFile.open("test.txt", O_READ)) {

You can't remove a file that has been opened read only.

Even after closing it? That seems like a strange restriction.

Can you remove it if you then open the file for write, and close it?

You can't remove a file that has been opened read only.

Even after closing it? That seems like a strange restriction.

Can you remove it if you then open the file for write, and close it?

There are two ways to remove a file.

The most common way is to use the volume object. You must not use this method if the file is open. There is no restriction on how the file was previously opened.

  sd.remove("junk.txt");

The second way uses a file object that is open for write. The error occurred when an attempt was made to remove a file opened read only.

If you open a file like this:

  file.open("junk.txt", O_WRITE);

You can remove it like this since the file was opened for write but an error will occur if it was opened read only.

  file.remove();

Using remove() with a file object is useful in conjunction with openNext() to find all files in a directory and delete selected files.

Another use for this form of remove() is to delete a temp file when it is no longer needed.