Skip to content

Commit 9e3ba26

Browse files
authored
SD - examples/listfilesEnhanced (#9206)
* add new example for enhanced file listing * adapt Pin * adapt due to clanf format check in line 14 * Adapt further clang findings * additional intention corrections * last clang format issues corrected * case-sensitive, eof line break
1 parent ccea728 commit 9e3ba26

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
Listfiles Enhanced
3+
4+
This example demonstrates how to list files on an SDcard in the following way:
5+
1) collect all directories
6+
2) build full path of directories and keep in mind
7+
3) then print all files with the help of the directorie pathes
8+
9+
Wiring:
10+
SDcard attached to SPI bus as follows:
11+
- MOSI: pin 11
12+
- MISO: pin 12
13+
- CLK : pin 13
14+
- CS : pin 4
15+
16+
Created:
17+
18. Nov 2024 by Frank Häfele
18+
19+
This example code is in the public domain.
20+
21+
*/
22+
#include <SPI.h>
23+
#include <SD.h>
24+
#include <vector>
25+
26+
#define SD_CS_PIN 4
27+
28+
29+
void dir(String path) {
30+
std::vector<String> directories;
31+
collectDirectories(path, directories);
32+
for (auto directory : directories) {
33+
printDirectoryName(directory.c_str(), 1);
34+
File fs = SD.open(directory);
35+
printFilesInDirectory(fs);
36+
Serial.println("\n===============");
37+
fs.close();
38+
}
39+
}
40+
41+
void setup() {
42+
// Open serial communications and wait for port to open:
43+
Serial.begin(115200);
44+
45+
Serial.print("\n\n==== List Directory ====\n\n");
46+
listDirectory();
47+
48+
Serial.println("done!");
49+
}
50+
51+
void loop() {
52+
// nothing happens after setup finishes.
53+
}
54+
55+
void listDirectory() {
56+
Serial.print("\n\nInitializing SD card...");
57+
if (!SD.begin(SD_CS_PIN)) {
58+
Serial.println("initialization failed!");
59+
return;
60+
}
61+
Serial.print("initialization successful.\n");
62+
Serial.print("List Files:\n");
63+
dir("/");
64+
}
65+
66+
67+
void printDirectoryName(const char *name, uint8_t level) {
68+
for (uint8_t i = 0; i < level; ++i) {
69+
Serial.print(" ");
70+
}
71+
Serial.println(name);
72+
}
73+
74+
75+
76+
// helper function: combine path
77+
String joinPath(const String &base, const String &name) {
78+
if (base.endsWith("/")) {
79+
return base + name;
80+
}
81+
return base + "/" + name;
82+
}
83+
84+
// recusive function to collect directory names
85+
void collectDirectories(const String &dirname, std::vector<String> &directories) {
86+
File root = SD.open(dirname);
87+
if (!root || !root.isDirectory()) {
88+
Serial.printf("Error: Cannot open %s\n", dirname.c_str());
89+
return;
90+
}
91+
directories.push_back(dirname);
92+
93+
File file = root.openNextFile();
94+
while (file) {
95+
if (file.isDirectory()) {
96+
String fullPath = joinPath(dirname, file.name());
97+
collectDirectories(fullPath, directories);
98+
}
99+
file = root.openNextFile();
100+
}
101+
root.close();
102+
}
103+
104+
// print filenames
105+
void printFileName(File file) {
106+
Serial.print("\t");
107+
Serial.printf("%30s", file.name());
108+
// files have sizes, directories do not
109+
Serial.print(" - ");
110+
Serial.print(file.size(), DEC);
111+
time_t cr = file.getCreationTime();
112+
time_t lw = file.getLastWrite();
113+
struct tm *tmstruct = localtime(&cr);
114+
Serial.printf("\tCREATION: %d-%02d-%02d %02d:%02d:%02d", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
115+
tmstruct = localtime(&lw);
116+
Serial.printf("\tLAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
117+
}
118+
119+
120+
// print files in directories
121+
void printFilesInDirectory(File dir) {
122+
while (true) {
123+
auto file = dir.openNextFile();
124+
if (!file) {
125+
// no more files
126+
break;
127+
}
128+
if (file.isDirectory()) {
129+
continue;
130+
} else {
131+
printFileName(file);
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)