Check if file exist on SD card
Hi,
I'm trying to check if a file name exist on the SD card so that I won't overwrite it. From what I saw, the Sdfile class doesn't have the exists() method - so is there a different way to check for this?
I tried to use SD.exists(filename) but it doesn't seem to work well. Is there any library I need to include in the code? Do I need to add the dir name to the filename somehow?
I will appreciate any help with this.
Thanks,
Gilad
Comments
Gilad, hi.
Doesn't the code increment the filename on each new write?
https://github.com/OpenBCI/OpenBCI_Cyton_Library/blob/master/examples/DefaultBoard/SD_Card_Stuff.ino#L310
The complete API is listed here,
https://www.arduino.cc/en/reference/SD
https://github.com/arduino-libraries/SD
William
Yes the code increment the filename on each write. But, after 256 files the names are repeated and files are erased.
I need a way to know the names of the files already on the SD. This is usually done by SD.exists but I don't understand how to use this with Cyton because this method is not included in the dedicated SD library for Cyton.
You can see references to 'exists' here in the code:
https://github.com/arduino-libraries/SD/blob/master/src/SD.cpp#L224
https://github.com/arduino-libraries/SD/blob/master/src/SD.cpp#L536
https://www.arduino.cc/en/Reference/SDexists
The code above appears to want to 'walk' the filepath/tree, including enclosing directories. Delimited by forward slashes (as on linux unix). So my suggestion would be to supply the complete pathname.
William
Seems from the API doc, you could also just call SD.open(filepath,mode), with mode set to FILE_READ. Then if successful, close the File object. Which is very similar to what the exists code does:
https://github.com/arduino-libraries/SD/blob/master/src/SD.cpp#L224
Thanks, the open file in read mode method is working.