• 递归拷贝目录与删除目录 WindowsAPI C++


    /*判断一个路径是否是已存在的目录*/
    bool IsDirectory(const std::wstring& pstrPath)
    {
    	DWORD dw = GetFileAttributes(pstrPath.c_str());
    	if (dw == INVALID_FILE_ATTRIBUTES)
    	{
    		return false;
    	}
    	return (dw & FILE_ATTRIBUTE_DIRECTORY) != 0;
    }
    
    /*复制目录及目录中的所有内容*/
    bool CopyFolder(const std::wstring& pstrFolder, const std::wstring& pstrDest)
    {
    	/*检查输入目录是否是合法目录*/
    	if (!IsDirectory(pstrFolder))
    	{
    		return false;
    	}
    	if (!IsDirectory(pstrDest))
    	{
    		CreateDirectoryW(pstrDest.c_str(), NULL);
    	}
    
    	std::wstring strFind = pstrFolder;
    	if (*strFind.rbegin() != L'\' &&
    		*strFind.rbegin() != L'/')
    	{
    		strFind.append(L"\");
    	}
    	strFind.append(L"*.*");
    	std::wstring strDest = pstrDest;
    	if (*strDest.rbegin() != L'\' &&
    		*strDest.rbegin() != L'/')
    	{
    		strDest.append(L"\");
    	}
    
    
    	/*打开文件查找,查看源目录中是否存在匹配的文件*/
    	/*调用FindFile后,必须调用FindNextFile才能获得查找文件的信息*/
    	WIN32_FIND_DATA wfd;
    	HANDLE hFind = FindFirstFileW(strFind.c_str(), &wfd);
    	if (hFind == INVALID_HANDLE_VALUE)
    	{
    		return false;
    	}
    	do
    	{
    		std::wstring strSubFolder;
    		std::wstring strDestFolder;
    		if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    		{
    			if (wfd.cFileName[0] == L'.')
    			{
    				continue;
    			}
    			else
    			{
    				strSubFolder = strFind.substr(0, strFind.length() - 3) + wfd.cFileName;
    				strDestFolder = strDest + +wfd.cFileName;
    				CopyFolder(strSubFolder, strDestFolder);
    			}
    		}
    		else
    		{
    			strSubFolder = strFind.substr(0, strFind.length() - 3) + wfd.cFileName;
    			strDestFolder = strDest + +wfd.cFileName;
    			CopyFileW(strSubFolder.c_str(), strDestFolder.c_str(), FALSE);
    		}
    	} while (FindNextFileW(hFind, &wfd));
    
    	/*删除空目录*/
    	FindClose(hFind);
    	return true;
    }
    
    /*删除目录及目录中的所有内容*/
    bool DeleteFolder(const std::wstring& pstrFolder, bool recursive)
    {
    	/*检查输入目录是否是合法目录*/
    	if (!IsDirectory(pstrFolder))
    	{
    		return false;
    	}
    
    	std::wstring strFind = pstrFolder;
    	if (*strFind.rbegin() != L'\' &&
    		*strFind.rbegin() != L'/')
    	{
    		strFind.append(L"\");
    	}
    	strFind.append(L"*.*");
    
    	/*打开文件查找,查看源目录中是否存在匹配的文件*/
    	/*调用FindFile后,必须调用FindNextFile才能获得查找文件的信息*/
    	WIN32_FIND_DATA wfd;
    	HANDLE hFind = FindFirstFileW(strFind.c_str(), &wfd);
    	if (hFind == INVALID_HANDLE_VALUE)
    	{
    		return false;
    	}
    	do
    	{
    		std::wstring strSubFolder;
    		if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    		{
    			if (wfd.cFileName[0] == L'.')
    			{
    				continue;
    			}
    			else if (recursive)
    			{
    				strSubFolder = strFind.substr(0, strFind.length() - 3) + wfd.cFileName;
    				DeleteFolder(strSubFolder, recursive);
    			}
    		}
    		else
    		{
    			strSubFolder = strFind.substr(0, strFind.length() - 3) + wfd.cFileName;
    			DeleteFileW(strSubFolder.c_str());
    		}
    	} while (FindNextFileW(hFind, &wfd));
    
    	/*删除空目录*/
    	FindClose(hFind);
    	return RemoveDirectoryW(pstrFolder.c_str()) == TRUE;
    }
    

      

  • 相关阅读:
    远程执行文件包含
    MSSQL2008 部署及开启远程连接
    schtasks 命令使用
    提权
    一个简单的Hibernate工具类HibernateUtil
    StandardServiceRegistryBuilder
    hibernate配置文件
    Hibernate入门
    mysql5.7.14安装与配置
    路漫漫兮其修远长,吾将上下而求索
  • 原文地址:https://www.cnblogs.com/xuhuajie/p/11841779.html
Copyright © 2020-2023  润新知