SD 06: List files
This example shows how to turn on the SD card. Firstly, delete all folder and all contained files. Secondly, create all directories specified in path. Finally, create files inside each subfolder and show the created directories and files. The rest of the program shows how to list the directories in several ways.

Required Materials

1 x Waspmote 1 x Battery 1 x SD card 1 x MiniUSB wire

Notes

- Remember that only 8.3 SHORT FILE NAME are valid for files and directories - Make sure Waspmote is switched off before inserting or removing the SD card. Otherwise, the SD card could be damaged. - Waspmote must not be switched off or reseted while there are ongoing read or write operations in the SD card. Otherwise, the SD card could be damaged and data could be lost. If you suspect that there may be some ongoing SD operations, wait a while until they are completed. - This example can be executed in Waspmote v12 and Waspmote v15

Code

1
/*
2
* ------ [SD_06] - List files inside the SD card --------
3
*
4
* Explanation: Turn on the SD card. Firstly, delete all folder and
5
* all contained files. Secondly, create all directories specified in
6
* path. Finally, create files inside each subfolder and show the created
7
* directories and files.
8
* The rest of the program shows how to list the directories in several ways.
9
*
10
* Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
11
* http://www.libelium.com
12
*
13
* This program is free software: you can redistribute it and/or modify
14
* it under the terms of the GNU General Public License as published by
15
* the Free Software Foundation, either version 3 of the License, or
16
* (at your option) any later version.
17
*
18
* This program is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
22
*
23
* You should have received a copy of the GNU General Public License
24
* along with this program. If not, see .
25
*
26
* Version: 3.0
27
* Design: David Gascón
28
* Implementation: Yuri Carmona
29
*/
30
31
32
// Define folder path
33
// All directory names MUST be defined
34
// according to 8.3 SHORT FILE NAME
35
char path[]="FOLDER1/FOLDER2/FOLDER3/";
36
37
// define variable
38
uint8_t sd_answer;
39
40
41
void setup()
42
{
43
// open USB port
44
USB.ON();
45
USB.println(F("SD_06 example"));
46
47
///////////////////////////////////////////
48
// 1. Set SD ON
49
///////////////////////////////////////////
50
SD.ON();
51
52
///////////////////////////////////////////
53
// 2. delete all directories at the beginning of execution
54
///////////////////////////////////////////
55
sd_answer = SD.rmRfDir("FOLDER1");
56
57
if( sd_answer == 1 )
58
{
59
USB.println(F("deleted folders and all contained files"));
60
}
61
else
62
{
63
USB.println(F("rmdir failed"));
64
}
65
66
///////////////////////////////////////////
67
// 3. create directories
68
///////////////////////////////////////////
69
sd_answer = SD.mkdir(path);
70
71
if( sd_answer == 1 )
72
{
73
USB.println(F("/FOLDER1/FOLDER2/FOLDER3/ created"));
74
}
75
else
76
{
77
USB.println(F("path NOT created"));
78
}
79
80
///////////////////////////////////////////
81
// 4. create a file inside FOLDER1/
82
///////////////////////////////////////////
83
sd_answer = SD.create("FOLDER1/file1");
84
85
if( sd_answer == 1 )
86
{
87
USB.println(F("/FOLDER1/file1 created"));
88
}
89
else
90
{
91
USB.println(F("/FOLDER1/file1 not created"));
92
}
93
94
///////////////////////////////////////////
95
// 5. create a file inside FOLDER1/FOLDER2/
96
///////////////////////////////////////////
97
sd_answer = SD.create("FOLDER1/FOLDER2/file2");
98
99
if( sd_answer == 1 )
100
{
101
USB.println(F("/FOLDER1/FOLDER2/file2 created"));
102
}
103
else
104
{
105
USB.println(F("/FOLDER1/FOLDER2/file2 not created"));
106
}
107
108
///////////////////////////////////////////
109
// 6. create a file inside FOLDER1/FOLDER2/FOLDER3
110
///////////////////////////////////////////
111
sd_answer = SD.create("FOLDER1/FOLDER2/FOLDER3/file3");
112
113
if( sd_answer == 1 )
114
{
115
USB.println(F("/FOLDER1/FOLDER2/FOLDER3/file3 created"));
116
}
117
else
118
{
119
USB.println(F("/FOLDER1/FOLDER2/FOLDER3/file3 not created"));
120
}
121
122
}
123
124
125
void loop()
126
{
127
////////////////////////////////////////////////////////
128
// 7 - list root directory: file size details by default
129
////////////////////////////////////////////////////////
130
USB.println(F("\n---------------------------"));
131
USB.println(F("list (file size details by default):"));
132
USB.println(F("---------------------------"));
133
SD.ls();
134
USB.println(F("---------------------------"));
135
136
delay(1000);
137
138
////////////////////////////////////////////////////////
139
// 8 - list root directory recursively
140
////////////////////////////////////////////////////////
141
USB.println(F("\n---------------------------"));
142
USB.println(F("list recursively:"));
143
USB.println(F("---------------------------"));
144
SD.ls( LS_R );
145
USB.println(F("---------------------------"));
146
147
delay(1000);
148
149
////////////////////////////////////////////////////////
150
// 9 - list root directory recursively showing file size
151
////////////////////////////////////////////////////////
152
USB.println(F("\n---------------------------"));
153
USB.println(F("list recursively showing file size:"));
154
USB.println(F("---------------------------"));
155
SD.ls( LS_R | LS_SIZE );
156
USB.println(F("---------------------------"));
157
158
delay(1000);
159
160
////////////////////////////////////////////////////////
161
// 10 - list root directory recursively showing date and file size
162
////////////////////////////////////////////////////////
163
USB.println(F("\n---------------------------"));
164
USB.println(F("list recursively showing date and file size:"));
165
USB.println(F("---------------------------"));
166
SD.ls( LS_R | LS_DATE | LS_SIZE );
167
USB.println(F("---------------------------"));
168
169
170
delay(5000);
171
}
172
173
174
Copied!

Output

1
H#
2
SD_06 example
3
deleted folders and all contained files
4
/FOLDER1/FOLDER2/FOLDER3/ created
5
/FOLDER1/file1 created
6
/FOLDER1/FOLDER2/file2 created
7
/FOLDER1/FOLDER2/FOLDER3/file3 created
8
---------------------------
9
list (file size details by default):
10
---------------------------
11
FOLDER1/
12
---------------------------
13
---------------------------
14
list recursively:
15
---------------------------
16
FOLDER1/
17
FOLDER2/
18
FOLDER3/
19
FILE3
20
FILE2
21
FILE1
22
---------------------------
23
---------------------------
24
list recursively showing file size:
25
---------------------------
26
FOLDER1/
27
FOLDER2/
28
FOLDER3/
29
FILE3 0
30
FILE2 0
31
FILE1 0
32
---------------------------
33
---------------------------
34
list recursively showing date and file size:
35
---------------------------
36
FOLDER1/ 2000-01-01 01:00:00
37
FOLDER2/ 2000-01-01 01:00:00
38
FOLDER3/ 2000-01-01 01:00:00
39
FILE3 2000-01-01 01:00:00 0
40
FILE2 2000-01-01 01:00:00 0
41
FILE1 2000-01-01 01:00:00 0
42
---------------------------
Copied!
Last modified 1yr ago