#include "stdafx.h"
#include <string>
#include <winbase.h>
BOOL IsExist(const std::wstring& strFilePath)
{
DWORD dwAttributes;
dwAttributes = ::GetFileAttributes(strFilePath.c_str());
if (dwAttributes == INVALID_FILE_ATTRIBUTES)
return FALSE;
else
return TRUE;
}
BOOL RemoveDirectoryEx(const std::wstring& strFilePath)
{
BOOL ret = FALSE;
if (!IsExist(strFilePath))
return ret;
WIN32_FIND_DATA fd;
std::wstring strFile = strFilePath + L"\\*.*";
HANDLE hFind = ::FindFirstFile(strFile.c_str(),&fd);
if (hFind == INVALID_HANDLE_VALUE)
{
return ret;
}
do
{
std::wstring name;
name = fd.cFileName;
if (fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
if ((name == L".") || (name == L".."))
continue;
BOOL result = RemoveDirectoryEx(strFilePath + L"\\" + name);
if (!result)
{
return FALSE;
// FindClose(hFind);
// return ret;
}
}
else
{
if (name == L"config.ini")
continue;
if (0 == DeleteFile((strFilePath + L"\\" + name).c_str()))
{
return FALSE;
}
}
}while (::FindNextFile(hFind,&fd));
FindClose(hFind);
// Remove empty directory;
bool result = RemoveDirectory(strFilePath.c_str());
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
if(argc != 2)
{
return 0;
}
else
{
std::wstring deletePath = argv[1];
TCHAR szSystemRoot[MAX_PATH];
ExpandEnvironmentStrings(TEXT("%SYSTEMROOT%"), szSystemRoot, _countof(szSystemRoot));
szSystemRoot[_countof(szSystemRoot) - 1] = 0;
std::wstring strSystemRoot = szSystemRoot;
std::wstring strSystemTemp = strSystemRoot+L"\\Temp";
RemoveDirectoryEx(deletePath);
}
}
删除文件夹
最新推荐文章于 2024-06-17 08:55:49 发布