]>
cloudbase.mooo.com Git - z180-stamp.git/blob - fatfs/documents/res/app2.c
1 /*------------------------------------------------------------/
2 / Delete a sub-directory even if it contains any file
3 /-------------------------------------------------------------/
4 / The delete_node() function is for R0.12+.
5 / It works regardless of FF_FS_RPATH.
10 TCHAR
* path
, /* Path name buffer with the sub-directory to delete */
11 UINT sz_buff
, /* Size of path name buffer (items) */
12 FILINFO
* fno
/* Name read buffer */
20 fr
= f_opendir(&dir
, path
); /* Open the sub-directory to make it empty */
21 if (fr
!= FR_OK
) return fr
;
23 for (i
= 0; path
[i
]; i
++) ; /* Get current path length */
27 fr
= f_readdir(&dir
, fno
); /* Get a directory item */
28 if (fr
!= FR_OK
|| !fno
->fname
[0]) break; /* End of directory? */
30 do { /* Make a path name */
31 if (i
+ j
>= sz_buff
) { /* Buffer over flow? */
32 fr
= 100; break; /* Fails with 100 when buffer overflow */
34 path
[i
+ j
] = fno
->fname
[j
];
35 } while (fno
->fname
[j
++]);
36 if (fno
->fattrib
& AM_DIR
) { /* Item is a sub-directory */
37 fr
= delete_node(path
, sz_buff
, fno
);
38 } else { /* Item is a file */
41 if (fr
!= FR_OK
) break;
44 path
[--i
] = 0; /* Restore the path name */
47 if (fr
== FR_OK
) fr
= f_unlink(path
); /* Delete the empty sub-directory */
54 int main (void) /* How to use */
62 f_mount(&fs
, _T("5:"), 0);
64 /* Directory to be deleted */
65 _tcscpy(buff
, _T("5:dir"));
67 /* Delete the directory */
68 fr
= delete_node(buff
, sizeof buff
/ sizeof buff
[0], &fno
);
70 /* Check the result */
72 _tprintf(_T("Failed to delete the directory. (%u)\n"), fr
);
75 _tprintf(_T("The directory and the contents have successfully been deleted.\n"), buff
);