CARME-M4 BSP  V1.5
app2.c
1 /*------------------------------------------------------------/
2 / Remove all contents of a directory
3 / This function works regardless of _FS_RPATH.
4 /------------------------------------------------------------*/
5 
6 
7 FRESULT empty_directory (
8  char* path /* Working buffer filled with start directory */
9 )
10 {
11  UINT i, j;
12  FRESULT fr;
13  DIR dir;
14  FILINFO fno;
15 
16 #if _USE_LFN
17  fno.lfname = 0; /* Eliminate LFN output */
18 #endif
19  fr = f_opendir(&dir, path);
20  if (fr == FR_OK) {
21  for (i = 0; path[i]; i++) ;
22  path[i++] = '/';
23  for (;;) {
24  fr = f_readdir(&dir, &fno);
25  if (fr != FR_OK || !fno.fname[0]) break;
26  if (fno.fname[0] == '.') continue;
27  j = 0;
28  do
29  path[i+j] = fno.fname[j];
30  while (fno.fname[j++]);
31  if (fno.fattrib & AM_DIR) {
32  fr = empty_directory(path);
33  if (fr != FR_OK) break;
34  }
35  fr = f_unlink(path);
36  if (fr != FR_OK) break;
37  }
38  path[--i] = '\0';
39  closedir(&dir);
40  }
41 
42  return fr;
43 }
44 
45 
46 
47 int main (void)
48 {
49  FRESULT fr;
50  FATFS fs;
51  char buff[64]; /* Working buffer */
52 
53 
54 
55  f_mount(&fs, "", 0);
56 
57  strcpy(buff, "/"); /* Directory to be emptied */
58  fr = empty_directory(buff);
59 
60  if (fr) {
61  printf("Function failed. (%u)\n", fr);
62  return 1;
63  } else {
64  printf("All contents in the %s are successfully removed.\n", buff);
65  return 0;
66  }
67 }
68 
69 
70 
FRESULT f_opendir(DIR *dp, const TCHAR *path)
Create a Directory Object.
Definition: ff.c:3160
FRESULT
File function return code.
Definition: ff.h:204
Directory object structure.
Definition: ff.h:162
File system object structure.
Definition: ff.h:88
#define AM_DIR
Definition: ff.h:445
FRESULT f_mount(FATFS *fs, const TCHAR *path, BYTE opt)
Mount/Unmount a Logical Drive.
Definition: ff.c:2392
TCHAR fname[13]
Definition: ff.h:194
Definition: ff.h:205
FRESULT f_unlink(const TCHAR *path)
Delete a File or Directory.
Definition: ff.c:3426
FRESULT f_readdir(DIR *dp, FILINFO *fno)
Read Directory Entries in Sequence.
Definition: ff.c:3238
File status structure.
Definition: ff.h:189
BYTE fattrib
Definition: ff.h:193