删除文件夹

#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);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值