SdFat file Deletion?

@fat16lib,

What is the correct sequence of calls to delete a file when using SdFat?

I see the warning on the file.remove() about 8.3, are you saying to use the lfn if it exists?

SdFat.remove() wants a 8.3 file name?

Is this a proper sequence?

SdFile file;
file.open(name,O_READ); 
file.remove(name);
? file.close();

// or just

SdFile file;

file.remove(name);

// or

SdFat sd; // open it also

SdFile file;

file.open(name,O_READ);
char buff[64];
file.getSFN(buf);
file.close();

sd.remove(buf);

Sorry, I'm missing something.

Edit, Added another Question:

How do I release an SdFat object correctly? I can see how to initialize an SdFat object:

SdFat *sd;
sd = new SdFat;

sd->begin(cs,SPI_FULL_SPEED);

But, when I am finished with it what is the correct method to shut it down? I have already closed all files using it.

Is this correct?

delete sd;

;

Chuck.

You can't remove a file that is open, so that is not the correct sequence.

Is this correct?

Yes. But, why are you creating an(other) instance using new?

SdFat sd;
sd.begin(cs,SPI_FULL_SPEED);

Then, you don't need to worry about deleting anything.

PaulS:
You can't remove a file that is open, so that is not the correct sequence.
Yes. But, why are you creating an(other) instance using new?

SdFat sd;

sd.begin(cs,SPI_FULL_SPEED);



Then, you don't need to worry about deleting anything.

I have built a FAT file system for EEPROM and FLASH that is being used for a html Server. (MEGA+Ethernet). It supports multiple 'domains' basically each EEPROM (24LCxx) is a 'domain' or I guess a 'drive'. The system is designed to allow mounting and dismounting of the domains in real time.

I am in the process of adding SdFat as another data source. My server code can server pages from the MEGA's FLASH, external EEPROM, SPIRAM, SPIFLASH. I want mounting and dismounting ability so that SDcards act the same as my other storage devices. I want all of the data devices to be equivalent for the Server.

Here is what navigating to server/eeprom/56/ cause the Mega to display:

my webserver code supports Server Side Includes (SSI), GET, PUT, POST. I had fun including EXEC functions that allow direct access to Arduino funtions and routines. This little server allows me a simple system to play with HTML client side code. But standard EEPROMs are only 64KB, and my SPIFLASH is only 8MB, I want the gigabytes of a SDcard.

I have read through many of the Doxygen html reference pages, but I have not found any pages that are overview or theoretical or best practice guides. Are there any guides for SdFat?

My question on the correct Creation/Destruction procedure came from the realization that SdFat supports multiple cards at the same time and that a 'current drive' can be selected by sd.chvol(). This presupposes a global instance or static indicator of a 'current' SdFat Object. My concern is that I have not identified the correct method to 'close' a SDcard. I want to support insertion and removal of the SDcard such that a simple inuse LED can be displayed for safe insertion and removal.

All of the examples do not have an explicate shutdown sequence. They just run off the end, and leave the sd() object active until reset or power down?

I would rather not corrupt a SDcard if possible.

Chuck

I would rather not corrupt a SDcard if possible.

A reasonable precaution. Perhaps fat16lib will weigh in on your other questions.

There are two ways to delete a file.

If the file is open for write use remove like this. You don't need to close the file after the remove call since the file no longer exists.

  // open file
  if (!file.open("My file.txt", O_RDWR)) {
    // handle open error
  }



  // done with file.  

  if (!file.remove()) {
    // handle error.
  }

If the file is not open use remove like this.

SdFat sd;


  // initialize.
  if (!sd.begin(CS_PIN)) {



  // later 
  if (!sd.remove("My File.txt")) {
    // handle error
  }

The caution about 8.3 names only applies if the file was created with a long name and you know the short name and use it to open the file. The directory entries for the long name will not be deleted.

So if the long name is "This is a lfn.txt" don't use THISIS~1.TXT to remove the file.

fat16lib:
There are two ways to delete a file.

If the file is open for write use remove like this. You don't need to close the file after the remove call since the file no longer exists.

  // open file

if (!file.open("My file.txt", O_RDWR)) {
    // handle open error
  }

// done with file.

if (!file.remove()) {
    // handle error.
  }




If the file is not open use remove like this.



SdFat sd;

// initialize.
  if (!sd.begin(CS_PIN)) {

// later
  if (!sd.remove("My File.txt")) {
    // handle error
  }




The caution about 8.3 names only applies if the file was created with a long name and you know the short name and use it to open the file. The directory entries for the long name will not be deleted.

So if the long name is "This is a lfn.txt" don't use THISIS~1.TXT to remove the file.

Thanks,
That explains my confusion about deleting files.

Do I need to do anything special to close down the file system before I eject the SDcard? Or just Close all open files and pop the card?

Chuck.

Someone can help me, how to read *.txt file using BOF (Begin Of File)??
Thanks...

BlacKey13:
Someone can help me, how to read *.txt file using BOF (Begin Of File)??
Thanks...

I don't understand your question.

Are you asking how to generate a list of all text files (*.txt) on a SDcard?

Are you asking how to read through a file from start to end?

Are you asking how to position the filepointer to the beginning of a file?

Chuck.

And what does reading a .txt file have to do with the subject of this thread?

@chucktodd, did you ever find an answer you believe/trust to your question...

Do I need to do anything special to close down the file system before I eject the SDcard? Or just Close all open files and pop the card? Chuck.

DaveEvans:
@chucktodd, did you ever find an answer you believe/trust to your question...

Never gained an Authoritative answer.

I have not encountered any problems, (memory leak, resets, etc..) by following these steps:

SdFat* sd;
sd = new SdFat;
if(!sd->begin(CSPin,SPI_FULL_SPEED)){
  // failed, heartache and Die!
  delete sd;
  }
else { //use sd, remembering to close all SdFile object that reference sd

  // before every Open, Delete, access to sd
  sd->chvol(); // set this SdFat as the 'current drive'

  SdFile* file;  // create pointer for 'file'
  file = new SdFile; // allocate memory, initialize SdFile Object
  file->open(fileNamecString,O_READ); // open file in 'current drive'

       // access file

  file->close();

  delete file; // run destructor for SdFile Object, free Heap variable;
  
  delete sd; // all done with SdCard, run Object Destructor, Free Heap Variable 
  }

Chuck.

Thank you.

Regarding....

chucktodd:
Do I need to do anything special to close down the file system before I eject the SDcard? Or just Close all open files and pop the card?

@chucktodd, while looking for something else, I stumbled on an answer from @fat16lib here:

There is no end() call, you only need to close all files before removing the card.

On a related note (not sure if it is of use to you), in that same thread, fat16lib said that the SdFat library can handle multiple "begins," so you can eject a card, insert a new one, and "begin" with the new card. Apparently the SD library has a bug that prevents doing that.