// FileOpt.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <WINDOWS.H>
#include <TCHAR.H>
using namespace std;
// 删除指定目录下所有文件及目录
BOOL DelDirFileOpt(string szPath)
{
WIN32_FIND_DATA wfd;
HANDLE hFind;
string sFullPath;
string sFindFilter;
DWORD dwAttributes = 0;
sFindFilter = szPath;
sFindFilter += _T("\\*.*");
if ((hFind = FindFirstFile(sFindFilter.c_str(), &wfd)) == INVALID_HANDLE_VALUE)
{
return FALSE;
}
do
{
if (_tcscmp(wfd.cFileName, _T(".")) == 0 ||
_tcscmp(wfd.cFileName, _T("..")) == 0 )
{
continue;
}
sFullPath = szPath;
sFullPath += _T('\\');
sFullPath += wfd.cFileName;
//去掉只读属性
dwAttributes = GetFileAttributes(sFullPath.c_str());
if (dwAttributes & FILE_ATTRIBUTE_READONLY)
{
dwAttributes &= ~FILE_ATTRIBUTE_READONLY;
SetFileAttributes(sFullPath.c_str(), dwAttributes);
}
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
printf("进入目录%s\n",sFullPath.c_str());
DelDirFileOpt(sFullPath.c_str());
RemoveDirectory(sFullPath.c_str());
printf("删除目录%s成功\n",sFullPath.c_str());
}
else
{
if ( _tcsicmp(wfd.cFileName, _T("index.dat")) == 0)
{
//WipeFile(szPath, wfd.cFileName);
}
DeleteFile(sFullPath.c_str());
printf("文件%s删除成功\n",sFullPath.c_str());
}
}while (FindNextFile(hFind, &wfd));
FindClose(hFind);
return TRUE;
}
#define MAX_NUM 262
int main(int argc, char* argv[])
{
char cCurPath[MAX_NUM] = {0};
GetCurrentDirectory(MAX_NUM,cCurPath);
string strDelPath = (string)cCurPath + (string)"\\testDir";
DelDirFileOpt(strDelPath.c_str());
return 0;
}