C++ 目录操作(Windows)

  

一、拷贝目录

bool CopyMyDirFlie(const std::string strMyDir, const std::string strDestDir)
{
	std::string strDir = strMyDir;
	std::string strNewDestDir = strDestDir;
	CreateDirectory(strDestDir.c_str(), NULL);
	if (strDir.at(strDir.length() - 1) != '\\')
	{
		strDir.append("\\");
	}
	if (strNewDestDir.at(strNewDestDir.length() - 1) != '\\')
	{
		strNewDestDir.append("\\");
	}
	WIN32_FIND_DATA wfd;
	HANDLE hFind = FindFirstFile((strDir + "*.*").c_str(), &wfd);
	if (hFind == INVALID_HANDLE_VALUE)
	{
		return false;
	}
    
    do
	{
		if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			if (_stricmp(wfd.cFileName, ".") != 0 && _stricmp(wfd.cFileName, "..") != 0)
			{
				CopyMyDirFlie(strDir + wfd.cFileName, strNewDestDir + wfd.cFileName);
			}
			else
			{
				CreateDirectory((strNewDestDir + wfd.cFileName).c_str(), NULL);
			}
		}
		else
		{
			CopyFile((strDir + wfd.cFileName).c_str(), (strNewDestDir + wfd.cFileName).c_str(), FALSE);
		}
	} while (FindNextFile(hFind, &wfd));
	FindClose(hFind);
	return true;
}

二、删除目录

bool RemoveMyDir(std::string strDir)
{
	std::string strOrignDir = strDir;
	if (strDir.at(strDir.length() - 1) != '\\')
	{
		strDir.append("\\");
	}
	//删除文件夹下所有文件
	WIN32_FIND_DATA wfd;
	HANDLE hFind = FindFirstFile((strDir + "*.*").c_str(), &wfd);
	if (hFind == INVALID_HANDLE_VALUE)
	{
		return false;
	}
    
    do
	{
		if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			if (_stricmp(wfd.cFileName, ".") != 0 && _stricmp(wfd.cFileName, "..") != 0)
			{
				RemoveMyDir(strDir + wfd.cFileName);
			}
		}
		else
		{
			DeleteFile((strDir + wfd.cFileName).c_str());
		}
	} while (FindNextFile(hFind, &wfd));
	FindClose(hFind);
	//删除该文件夹
	if (!RemoveDirectory(strDir.c_str()))
	{
		DWORD dCode = GetLastError();
		if (dCode == ERROR_DIR_NOT_EMPTY)
		{
			RemoveMyDir(strDir.c_str());
		}
		return false;
	}
	return true;
}

三、创建隐藏文件夹

void CreateHideDirectory(std::string strDir)
{
	if (!PathIsDirectory(strDir.c_str()))
	{
		CreateDirectory(strDir.c_str(), NULL);
		SetFileAttributes(strDir.c_str(), FILE_ATTRIBUTE_HIDDEN);		//设置为隐藏文件夹
	}
}

四、判断目录是否存在五

bool CheckFolderExist(const string &strPath)
{
	WIN32_FIND_DATA  wfd;
	bool rValue = false;
	HANDLE hFind = FindFirstFile(strPath.c_str(), &wfd);
	if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
	{
		rValue = true;
	}
	FindClose(hFind);
	return rValue;
}

 五、获取目录下所有文件

int MyGetAllFiles(std::string strDir, std::vector<std::string>& filesVec)
{
	WIN32_FIND_DATA findData;
	std::string strFindFile = strDir + "\\*";
	HANDLE hFind = FindFirstFile(strFindFile.c_str(), &findData);
	if (hFind == INVALID_HANDLE_VALUE)
	{
		return -1;
	}
	do
	{
		if (std::string(findData.cFileName).compare(".") == 0 ||
			std::string(findData.cFileName).compare("..") == 0)continue;
		if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
			//如果是目录
			std::string strDir1 = strDir + "\\";
			strDir1.append(std::string(findData.cFileName).c_str());
			MyGetAllFiles(strDir1, filesVec);
		}
		else
		{
			filesVec.push_back(strDir + "\\" + std::string(findData.cFileName));
		}
	} while (FindNextFile(hFind, &findData));
	return 0;
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_君莫笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值