1

I am trying to count files. I have one hidden file from Windows10 so i am doing "-1" but I want to open the last file and can't work out how, any help? Please, Thanks

#include <SD.h>
int entryCount = 0;
File root;
int total = 0;
int Lfile = 0;
void setup()
{
 Serial.begin(9600);
 pinMode(10, OUTPUT);
 SD.begin(4);
 root = SD.open("/");
 printDirectory(root, 0);
 root.close();
 Serial.println();
 Serial.print(total-1);
 Lfile = total-1;
 File dataFile = SD.open(Lfile);
 // if the file is available, write to it:
 if (dataFile) {
 while (dataFile.available()) {
 Serial.write(dataFile.read());
 }
 dataFile.close();
 }
 // if the file isn't open, pop up an error:
 else {
 Serial.println();
 Serial.println("error opening last file");
 }
}
void loop()
{
 // nothing happens after setup finishes.
}
void printDirectory(File dir, int numTabs)
{
 while (true)
 {
 File entry = dir.openNextFile();
 if (! entry)
 {
 if (numTabs == 0)
 return;
 }
 for (uint8_t i = 0; i < numTabs; i++)
 Serial.print('\t');
 Serial.println(entry.name());
 total = entryCount;
 Serial.print(entryCount++);
 Serial.print('\t');
 entry.close();
 }
}
st2000
7,5032 gold badges13 silver badges19 bronze badges
asked Jul 10, 2018 at 22:58
2
  • what does last file mean? Commented Jul 10, 2018 at 23:51
  • sd card have files log01.txt, log02.txt, etc. Last file is last file which was created Commented Jul 11, 2018 at 0:19

1 Answer 1

1

Consider using the exist() method found in the SD.h library you already are including in your program. Assuming you will create no more than 99 files and the last file created will have the highest 2 digit value as part of its name, test for the existence of the file log99.txt. If not found, decrement the 2 digit number and test for the existence of file log98.txt. Repeat this process until you find the newest file created.

answered Jul 11, 2018 at 0:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.