#include <iostream.h> #include >string.h> #include <stdlib.h> #include <windows.h> #include <stdio.h> #include <shellapi.h> #include <fstream.h> #include <string> using namespace std;void main() {
char* a=".";
char* b="";
WIN32_FIND_DATA FileData,FileData_0;
HANDLE hSearch,hSearch_0;
BOOL fFinished=FALSE;
hSearch=FindFirstFile("C:\\experiment\\results_stat\\*.*",&FileData); //输入特定文件的绝对路径名
if(hSearch==INVALID_HANDLE_VALUE)
{
return;
}while(!fFinished){
if(FileData.cFileName[0]!=a[0]){
b=FileData.cFileName;string addr=string("C:\\experiment\\results_stat\\")+string(b)+string("\\")+string("*.*"); //输入特定文件的绝对路径名hSearch_0=FindFirstFile(addr.c_str(),&FileData_0);while(FindNextFile(hSearch_0, &FileData_0)){if(FileData_0.cFileName[0]!=a[0]){string addr_0=string("C:\\experiment\\results_stat\\")+string(b)+string("\\")+FileData_0.cFileName; //输入特定文件的绝对路径名DeleteFile(addr_0.c_str()); //清空特定文件夹中的子文件夹中的所有文件}}
}if(!FindNextFile(hSearch,&FileData)){
if(GetLastError()==ERROR_NO_MORE_FILES){fFinished=TRUE;}else{printf("Couldn't find next file.");return;}
}
}FindClose(hSearch);FindClose(hSearch_0);
} 如果文件的属性石只读的可以将只读属性去掉,然后在删除,下面是去掉只读属性的方法:
if ((fileAttributes & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY)
{
fileAttributes &= ~FILE_ATTRIBUTE_READONLY;
SetFileAttributes(filename);
}