SAMV71 Xplained Ultra Software Package 1.0

app2.c

00001 /*------------------------------------------------------------/
00002 / Remove all contents of a directory
00003 / This function works regardless of _FS_RPATH.
00004 /------------------------------------------------------------*/
00005 
00006 
00007 FRESULT empty_directory (
00008     char* path      /* Working buffer filled with start directory */
00009 )
00010 {
00011     UINT i, j;
00012     FRESULT fr;
00013     DIR dir;
00014     FILINFO fno;
00015 
00016 #if _USE_LFN
00017     fno.lfname = 0; /* Disable LFN output */
00018 #endif
00019     fr = f_opendir(&dir, path);
00020     if (fr == FR_OK) {
00021         for (i = 0; path[i]; i++) ;
00022         path[i++] = '/';
00023         for (;;) {
00024             fr = f_readdir(&dir, &fno);
00025             if (fr != FR_OK || !fno.fname[0]) break;
00026             if (_FS_RPATH && fno.fname[0] == '.') continue;
00027             j = 0;
00028             do
00029                 path[i+j] = fno.fname[j];
00030             while (fno.fname[j++]);
00031             if (fno.fattrib & AM_DIR) {
00032                 fr = empty_directory(path);
00033                 if (fr != FR_OK) break;
00034             }
00035             fr = f_unlink(path);
00036             if (fr != FR_OK) break;
00037         }
00038         path[--i] = '\0';
00039         closedir(&dir);
00040     }
00041 
00042     return fr;
00043 }
00044 
00045 
00046 
00047 int main (void)
00048 {
00049     FRESULT fr;
00050     FATFS fs;
00051     char buff[64];    /* Working buffer */
00052 
00053 
00054 
00055     f_mount(&fs, "", 0);
00056 
00057     strcpy(buff, "/");  /* Directory to be emptied */
00058     fr = empty_directory(buff);
00059 
00060     if (fr) {
00061         printf("Function failed. (%u)\n", fr);
00062         return fr;
00063     } else {
00064         printf("All contents in the %s are successfully removed.\n", buff);
00065         return 0;
00066     }
00067 }
00068 
00069 
00070 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines