Reprint Please specify source: http://blog.csdn.net/u011833609/article/details/47803551
====================================================================================================//function Name: f_ deldir//function: Remove a folder, including itself and its subfolders, sub-file//input: const TCHAR *PATH---Pointer to the null-terminated string object to remove//output: none//return value: Fr_ok (0): function succeeded//Fr_no_file: Unable to find file or directory//Fr_no_path: Unable to find path//Fr_invalid_name : Invalid file name//fr_invalid_drive: Drive letter Illegal//fr_denied: The function was rejected for the following reasons://object property is read-only; The directory is not empty;//current directory. Fr_not_ready: The disk drive does not work because there is no media or other reason in the drive//fr_write_protected: Media write protection//Fr_disk_err : Function failed due to disk run error//Fr_int_err: Function failed due to a wrong FAT structure or internal error//fr_not_enabled: logical Drive NOT Workspace//Fr_no_filesystem: No valid FAT volume on drive//fr_locked: function rejected due to file sharing mechanism (_fs_share)//remark: F The _deldir function is used to remove a folder and its subfolders, sub-files, but not to remove objects that are already open. //====================================================================================================fresult f_deldir (const TCHAR *path) {Fresult res; Dir dir; /* Folder Object *///36 bytes Filinfo fno; /* File properties *///32 bytes TCHAR FILE[_MAX_LFN + 2] = {0}; #if _use_lfn TCHAR LNAME[_MAX_LFN + 2] = {0}; #endif #if _ USE_LFN fno.lfsize = _MAX_LFN; Fno.lfname = lname; Initial value must be assigned #endif//Open Folder res = F_opendir (&dir, path); Read the folder contents continuously while (res = = FR_OK) && (Fr_ok = = F_readdir (&dir, &FNO))) {//if "." or ".." folder, skip if (0 = = strlen (fno.fname)) break; If the read file name is null if (0 = = strcmp (Fno.fname, ".")) Continue If you read the file name as the current folder if (0 = = strcmp (Fno.fname, "...")) Continue If the file name is read as a previous folder memset (file, 0, sizeof (file)), #if _USE_LFN sprintf ((char*) files, "%s/%s", Path, (*FN O.lfname)? Fno.lfname:fno.fname); #else sprintf ((char*) file, "%s/%s", Path, fno.fname); #endif if (Fno.fattrib & AM _dir) {//If folder, recursive delete res = f_deldir (file); } else {//If the file is deleted, delete res = F_unlink (file) directly; }}//delete itself if (res = = FR_OK) res = f_unlink (path); return res;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
fatfs-f_deldir-Delete a folder (delete subfolders and files along with itself)