app2.c 1.5 KB

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