version 2.8
app2.c
1 /*------------------------------------------------------------/
2 / Remove all contents of a directory
3 / This function works regardless of FF_FS_RPATH.
4 /------------------------------------------------------------*/
5 
6 
7 FILINFO fno;
8 
9 FRESULT empty_directory (
10  char* path /* Working buffer filled with start directory */
11 )
12 {
13  UINT i, j;
14  FRESULT fr;
15  DIR dir;
16 
17  fr = f_opendir(&dir, path);
18  if (fr == FR_OK) {
19  for (i = 0; path[i]; i++) ;
20  path[i++] = '/';
21  for (;;) {
22  fr = f_readdir(&dir, &fno);
23  if (fr != FR_OK || !fno.fname[0]) break;
24  if (_FS_RPATH && fno.fname[0] == '.') continue;
25  j = 0;
26  do
27  path[i+j] = fno.fname[j];
28  while (fno.fname[j++]);
29  if (fno.fattrib & AM_DIR) {
30  fr = empty_directory(path);
31  if (fr != FR_OK) break;
32  }
33  fr = f_unlink(path);
34  if (fr != FR_OK) break;
35  }
36  path[--i] = '\0';
37  closedir(&dir);
38  }
39 
40  return fr;
41 }
42 
43 
44 
45 int main (void)
46 {
47  FRESULT fr;
48  FATFS fs;
49  char buff[256]; /* Working buffer */
50 
51 
52 
53  f_mount(&fs, "", 0);
54 
55  strcpy(buff, "/"); /* Directory to be emptied */
56  fr = empty_directory(buff);
57 
58  if (fr) {
59  printf("Function failed. (%u)\n", fr);
60  return fr;
61  } else {
62  printf("All contents in the %s are successfully removed.\n", buff);
63  return 0;
64  }
65 }
66 
67 
68