• 递归拷贝目录与删除目录 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;
    }
    

      

  • 相关阅读:
    Redis 基础、高级特性与性能调优
    CentOS 7安装新版RabbitMQ解决Erlang 19.3版本依赖
    Centos7中docker开启远程访问
    基于Docker+Jenkins+Gitlab搭建持续集成环境
    Gitlab利用Webhook实现Push代码后的jenkins自动构建
    在jenkins中调用maven的变量
    docker中gitlab-runner配置
    搭建docker私有仓库
    ku8eye 安装概览
    配置docker官方源并用yum安装docker
  • 原文地址:https://www.cnblogs.com/xuhuajie/p/11841779.html
Copyright © 2020-2023  润新知