app2.c 1.4 KB

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