#include<iostream>
#include<string>
#include<io.h>
using namespace std;
//递归删除指定文件夹下所有的文件(目前好像删不了文件夹)
int removeDir(string dirPath)
{
struct _finddata_t fb; //find the storage structure of the same properties file.
string path;
long handle;
int resultone;
int noFile; // the tag for the system's hidden files
noFile = 0;
handle = 0;
path = dirPath + "/*";
handle = _findfirst(path.c_str(), &fb);
//find the first matching file
if (handle != -1)
{
//find next matching file
while (0 == _findnext(handle, &fb))
{
// "." and ".." are not processed
noFile = strcmp(fb.name, "..");
if (0 != noFile)
{
path.clear();
path = dirPath + "/" + fb.name;
//fb.attrib == 16 means folder
if (fb.attrib == 16)
{
removeDir(path);
}
else
{
//not folder, delete it. if empty folder, using _rmdir istead.
remove(path.c_str());
}
}
}
// close the folder and delete it only if it is closed. For standard c, using closedir instead(findclose -> closedir).
// when Handle is created, it should be closed at last.
_findclose(handle);
}
return 0;
}
void main() {
removeDir(".\\test");
}