Skip to content

Commit 6f0ea10

Browse files
committed
Adding SD.remove(file) and another example.
1 parent 5af5619 commit 6f0ea10

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

libraries/SD/SD.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,26 @@ boolean callback_openPath(SdFile& parentDir, char *filePathComponent,
294294
295295
*/
296296
if (isLastComponent) {
297-
SDClass *p_MemoryCard = static_cast<SDClass*>(object);
298-
p_MemoryCard->file.open(parentDir, filePathComponent,
299-
p_MemoryCard->fileOpenMode);
300-
p_MemoryCard->c = -1;
297+
SDClass *p_SD = static_cast<SDClass*>(object);
298+
p_SD->file.open(parentDir, filePathComponent, p_SD->fileOpenMode);
299+
p_SD->c = -1;
301300
// TODO: Return file open result?
302301
return false;
303302
}
304303
return true;
305304
}
306305

307306

307+
boolean callback_remove(SdFile& parentDir, char *filePathComponent,
308+
boolean isLastComponent, void *object) {
309+
if (isLastComponent) {
310+
SdFile::remove(parentDir, filePathComponent);
311+
return false;
312+
}
313+
return true;
314+
}
315+
316+
308317

309318
/* Implementation of class used to create `SDCard` object. */
310319

@@ -415,4 +424,8 @@ boolean SDClass::mkdir(char *filepath) {
415424
return walkPath(filepath, root, callback_makeDirPath);
416425
}
417426

427+
void SDClass::remove(char *filepath) {
428+
walkPath(filepath, root, callback_remove);
429+
}
430+
418431
SDClass SD;

libraries/SD/SD.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class SDClass {
6060
// Create the requested directory heirarchy--if intermediate directories
6161
// do not exist they will be created.
6262
boolean mkdir(char *filepath);
63+
64+
// Delete the file.
65+
void remove(char *filepath);
6366

6467
private:
6568
SdFile file;

libraries/SD/examples/Files/Files.pde

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <SD.h>
2+
3+
File f;
4+
5+
void setup()
6+
{
7+
Serial.begin(9600);
8+
Serial.print("Initializing SD card...");
9+
SD.begin();
10+
Serial.println("done.");
11+
12+
if (SD.exists("example.txt")) Serial.println("example.txt exists.");
13+
else Serial.println("example.txt doesn't exist.");
14+
15+
Serial.println("Creating example.txt...");
16+
f = SD.open("example.txt", true);
17+
f.close();
18+
19+
if (SD.exists("example.txt")) Serial.println("example.txt exists.");
20+
else Serial.println("example.txt doesn't exist.");
21+
22+
Serial.println("Removing example.txt...");
23+
SD.remove("example.txt");
24+
25+
if (SD.exists("example.txt")) Serial.println("example.txt exists.");
26+
else Serial.println("example.txt doesn't exist.");
27+
}
28+
29+
void loop()
30+
{
31+
}
32+
33+

0 commit comments

Comments
 (0)