Go Down

Topic: How to read the last file from sd card? (Read 1 time) previous topic - next topic

vvadim

Hi.
I write some txt files on sd card with timestamp.
I use SD.h library.
So I can read what first file is written down on sd card
Code: [Select]
#include <SD.h>
File myFile;
 
void setup()
{
  Serial.begin(9600);
  pinMode(53, OUTPUT);
  SD.begin(53);
  myFile = SD.open("/");
  File entry =  myFile.openNextFile();
  Serial.print(entry.name());
}
 
void loop()

}


So I can read what files are written down on sd  card
Code: [Select]

#include <SD.h>
Sd2Card card;
SdVolume volume;
SdFile myFile;
const int chipSelect = 53;   

void setup()
{
  Serial.begin(9600); 
  pinMode(53, OUTPUT); 
  if (!card.init(SPI_HALF_SPEED, chipSelect))
  {   
    return;
  }   
  if (!volume.init(card))
  {
    return;
  } 
  Serial.println("\n name, date: ");
  myFile.openRoot(volume);
  myFile.ls(LS_R | LS_DATE);
}

void loop(void)

}


How to learn what file  is written down by the last and how many all is written down files?

My task to learn what file is written down by the last and to show its contents to the display. The following purpose buttons up or down listing files. As I understand for this purpose it is necessary to know how many files is written down on the card.

I ask for help.
Thanks in advance.

Nick_Pyner

#1
Dec 25, 2015, 12:14 am Last Edit: Dec 25, 2015, 12:17 am by Nick_Pyner
As I understand for this purpose it is necessary to know how many files is written down on the card.
I don't think that is right. The ReadWrite example in the IDE implies you just need to know the name of the file. If you don't know the name of the file, there is a listfiles programme in the same section.

If by display you mean the serial monitor, you might as well use Dumpfile. It is probably simpler as well as more appropriate.

vvadim

Thanks Nick.
But files have names by date and they are created not every day. Therefore it is impossible to remember them.
It would be desirable to have function which, for example on pressing of the button, showed the written-down file, last on time.
By display I meant lcd 20 x 4.

I thought to make calculation of number of the written-down files.
But there is one problem - the file during the day corresponding to its name can be rewritten several times.

Nick_Pyner

As I said, there is a listfiles programme in the same section.

vvadim

listfiles output all files from the first on time and date of creation and doesn't give the chance to count number of files on the card.
I can see my files  in Serial but I can't touch them.

vvadim

I understood that I won't be able to solve the problem with SD library.
Therefore started studying other libraries. And in SdFat found the solution on the basis of LongFileName example. Thanks to the author!

Go Up