C++:删除文件夹及其内部文件

1.删除的路径
strPath.Format(L"F:\\RawImage\\Station_%d",1); DeleteFolderTotal(strPath);

2.确定要删除的文件夹是哪些:

void ConfigFileCpyDlg::DeleteFolderTotal(CString cstrDirectoryPath)
{
	CFileFind cFinder;
	BOOL bFile = cFinder.FindFile(cstrDirectoryPath + L"\\*.*");//查找这个文件夹下有多少个文件夹
	bool bNeedRemove = false;
	std::vector<CString> vFullName;
	while (bFile)
	{
		bFile = cFinder.FindNextFile();
		if (cFinder.IsDots())
		{
			continue;
		}
		if (cFinder.IsDirectory())
		{
			CString strFullName = cFinder.GetFilePath();
			vFullName.push_back(strFullName);//每个文件夹的名字放在这个容器vFullName里
		}
	}
	int nSize = (int)vFullName.size();
	if (nSize < 100)  //文件夹数量<100就不删除了
	{
		return;
	}
	nSize = -95;
	for (int i = 0; i < nSize; i++)
	{
		CString strPath = vFullName[i];
		gSystemLogger.QueueUpLogAndReport(strPath.GetBuffer(), LV_EVENT);
		DeleteDirectory(strPath);  //删除排在前面的文件夹及其内部文件
	}
}

3.删除目标文件夹及其内部的文件

void ConfigFileCpyDlg::DeleteDirectory(CString strFolder)
{
	CString cstrFolderName = strFolder;
	CString cstrPath;//文件夹下的文件的路径
	CFileFind finder;
	//strFolder = L"F:\\RawImage\\JIG_1\\2019-03-21-15-49-35";
	cstrPath.Format(L"%s\\*.*", strFolder);//这个是具体的内部文件
	strFolder.Append(L"\\");

	BOOL bFindFolder = finder.FindFile(cstrPath);
	if (!bFindFolder)
	{
		return;
	}
	while (bFindFolder)
	{
		bFindFolder = finder.FindNextFile();
		if (finder.IsDots())
		{
			continue;
		}
		else
		{
			CString strFilelName = finder.GetFileName();
			CString strFullName = strFolder + strFilelName;
			CFile tempFile;
			tempFile.Remove(strFullName);//获得文件的名字,然后删除
		}
	}
	RemoveDirectory(cstrFolderName);//全部删除之后,才能删除文件夹,文件夹需要为空才能被删除
}

4.后来发现上文的那个只是删除某个文件夹,假设文件夹内部还有文件夹,就不能正常使用,于是写了递归的算法

bool ConfigFileCpyDlg::DeleteDirectory(CString DirName, BOOL bRemoveRootDir)
{
CFileFind tempFind;
CString csZRootDir;
csZRootDir.Format(L"%s\.", DirName.GetBuffer(0));

BOOL IsFinded = (BOOL)tempFind.FindFile(csZRootDir.GetBuffer(0));
while (IsFinded)
{
	IsFinded = (BOOL)tempFind.FindNextFile();
	if (!tempFind.IsDots())
	{
		CString csFileName;
		csFileName.Format(L"%s", tempFind.GetFileName().GetBuffer(0));
		if (tempFind.IsDirectory())
		{
			CString tmp;
			tmp.Format(L"%s\\%s", DirName.GetBuffer(0), csFileName.GetBuffer(0));
			DeleteDirectory(tmp, TRUE);
		}
		else
		{
			CString tmp;
			tmp.Format(L"%s\\%s", DirName, csFileName.GetBuffer(0));
			DeleteFile(tmp.GetBuffer(0));
		}
	}
}
tempFind.Close();
if (bRemoveRootDir)
{
	if (!RemoveDirectory(DirName))
	{
		return FALSE;
	}
}
return TRUE;

}
函数的标记位参数就是用于删除选定的文件夹

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Qt C++ 删除多级文件夹包括文件可以使用QDir类和QFile类。首先,我们可以使用QDir::removeRecursively()函数来删除整个目录,包括所有子目录和文件。然后,我们可以使用QFile::remove()函数删除单个文件。 以下是一个示例代码,可以删除多级文件夹包括文件: ```cpp #include <QCoreApplication> #include <QDir> #include <QFile> #include <QDebug> void deleteFolder(const QString& folderPath) { QDir dir(folderPath); // 判断文件夹是否存在 if (!dir.exists()) { return; } // 获取文件夹内所有子目录和文件 QFileInfoList fileList = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot); // 遍历所有子目录和文件 foreach (QFileInfo fileInfo, fileList) { if (fileInfo.isDir()) { // 如果是目录,则递归删除 deleteFolder(fileInfo.filePath()); } else { // 如果是文件,则直接删除 QFile::remove(fileInfo.filePath()); } } // 删除目录 if (dir.rmdir(folderPath)) { qDebug() << "Folder deleted successfully!"; } else { qDebug() << "Failed to delete folder!"; } } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString folderPath = "C:/TestFolder"; // 要删除文件夹路径 deleteFolder(folderPath); return a.exec(); } ``` 在这个示例,我们首先使用QDir类获取文件夹内所有子目录和文件,然后遍历所有子目录和文件,如果是目录则递归删除,如果是文件则直接删除。最后,我们使用QDir::rmdir()函数删除目录本身。 请注意,删除操作是不可逆的,因此在使用此函数之前,请确定您已经备份了需要保留的文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值