vc文件删除问题
我要删除一文件夹下的所有文件,此文件夹下包含一个或多个子文件夹怎么做啊????
#include <afx.h>
#include<iostream>
using namespace std;
void MessageErr(LONG re)
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,\
NULL,re,0,(LPTSTR) &lpMsgBuf,0,NULL);
MessageBox( NULL, (LPCTSTR)lpMsgBuf, \"Error\", MB_OK | MB_ICONINFORMATION );
LocalFree( lpMsgBuf );
}
BOOL DelDir(LPTSTR path)
{
WIN32_FIND_DATA wfd;
LONG re=ERROR_SUCCESS;
BOOL b=TRUE;
LPTSTR file=NULL;
HANDLE hFile = FindFirstFile(path,&wfd);
if((wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)!=FILE_ATTRIBUTE_DIRECTORY)
{
// DeleteFile(path);
printf(\"删除文件%s\n\",path);
goto SUCCEESS;
}
file=(LPTSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,lstrlen(path)+lstrlen(\"\\*.*\")+1);
lstrcat(file,path);
lstrcat(file,\"\\*.*\");
for(hFile = FindFirstFile(file,&wfd);b==TRUE;b=FindNextFile(hFile,&wfd))
{
if((wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)!=FILE_ATTRIBUTE_DIRECTORY)
{
printf(\"删除文件%s\\%s\n\",path,wfd.cFileName);
}
else if(!lstrcmp(wfd.cFileName,\".\")||!lstrcmp(wfd.cFileName,\"..\"))
continue;
else
{
LPTSTR filename=(LPTSTR)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,lstrlen(file)+lstrlen(\"\\\")+lstrlen(wfd.cFileName)+1);
lstrcat(filename,path);
lstrcat(filename,\"\\\");
lstrcat(filename,wfd.cFileName);
BOOL del=DelDir(filename);
HeapFree(GetProcessHeap(),HEAP_NO_SERIALIZE,filename);
if(!del)
goto FAIL;
}
}
re=GetLastError();
if(ERROR_NO_MORE_FILES!=re&&re!=ERROR_SUCCESS)
{
MessageErr(re);
goto FAIL;
}
printf(\"删除文件夹%s\n\",path);
goto SUCCEESS;
FAIL:
HeapFree(GetProcessHeap(),HEAP_NO_SERIALIZE,file);
return FALSE;
SUCCEESS:
HeapFree(GetProcessHeap(),HEAP_NO_SERIALIZE,file);
return TRUE;
}
void main()
{
system(\"pause\");
if(!DelDir(\"E:\\wfpb\"))
{
printf(\"没有删除完全,请手动删除\n\");
}
else
{
printf(\"成功完全删除\n\");
}
system(\"pause\");
}
[此贴子已经被作者于2007-5-17 10:11:12编辑过]