C 实现删除非空文件夹
简介:
[cpp] view plain copy
print?
/*
文件名: rd.c
----------------------------------------------------
c中提供的对文件夹操作的函数,只能对空文件夹进行
删除,这使很多初学者在编码过程中产生许多困扰,我也
很不爽这件事情,所以编写这个对非空文件夹进行删除的
函数,仅供参考。
-
-
-
-
- #include <stdio.h>
- #include <io.h>
- #include <string.h>
- #include <direct.h>
-
- int removeDir(const char* dirPath)
- {
-
- struct _finddata_t fb;
- char path[250];
- long handle;
- int resultone;
- int noFile;
-
- noFile = 0;
- handle = 0;
-
-
-
- strcpy(path,dirPath);
- strcat (path,"/*");
-
- handle = _findfirst(path,&fb);
-
- if (handle != 0)
- {
-
- while (0 == _findnext(handle,&fb))
- {
-
- noFile = strcmp(fb.name,"..");
-
- if (0 != noFile)
- {
-
- memset(path,0,sizeof(path));
- strcpy(path,dirPath);
- strcat(path,"/");
- strcat (path,fb.name);
-
- if (fb.attrib == 16)
- {
- removeDir(path);
- }
-
- else
- {
- remove(path);
- }
- }
- }
-
-
- _findclose(handle);
- }
-
- resultone = rmdir(dirPath);
- return resultone;
- }