刪除目錄下所有包含某個字符串的所有文件夾(包括子目錄下)

問題: 刪除本地某一目錄下所有的名稱中包含指定字符串的目錄,如刪除一個目錄下所有包含“Debug”的目錄:

  1. 聲明並實現一個空函數:BOOL RemoveSpecifiedDir(LPCTSTR lpszDirPath, LPCTSTR lpszSpecifiedDir)
  2. 假設此函數已經完成,編寫一個測試方法以測試此函數,需要測試指定目錄在執行程序所在目錄下的情況,也就是說把exe放到任意一個目錄中,執行exe後就能把該目錄下所有指定目錄刪除。
  3. 實現此函數BOOL RemoveSpecifiedDir(LPCTSTR lpszDirPath, LPCTSTR lpszSpecifiedDir)
  4. 用已經寫好的測試方法測試此函數

解決:要求在運行.exe程序時,在此時的當前目錄下對於我們指定的需要刪除的某個文件夾(包括它的子目錄也有此文件夾時也需要刪除) 這是一個遞歸處理的過程,其中分爲查找到該文件夾,刪除該文件夾(非空文件夾需要先將文件夾中的內容刪除再移除該文件夾),然後遞歸繼續以上處理從而達到在該目錄以及該目錄的各個子目錄下刪除指定文件夾。這裏我們使用的是MFC,可以在Edit中輸入字符,從而指定你想要刪除的文件夾,以下是查找文件過程

//查找文件
BOOL CDeletDlg::RemoveSpecifiedDir(string path, CString lpszSpecifiedDir)
{
	long nFile = 0;
	struct _finddata_t fileinfo; 
	string p; 
	if ((nFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1) {
		do {
			if ((fileinfo.attrib & _A_SUBDIR)) 
			{  //比較文件類型是否是文件夾
				if (strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0) 
				{
					path += "\\";
					CString szPath = path.c_str();
					szPath  += lpszSpecifiedDir; // 獲得與.exe執行程序相同目錄下指定包含字符串的目錄名
					BOOL bFlag = PathFileExists(szPath);
					if(bFlag)
					{
						DeleteDirectory(szPath);// 刪除該文件同事刪除其子文件
						bExit = true; // 確定是否存在該文件供刪除
					}
					else
					{
						RemoveSpecifiedDir(p.assign(path).append(fileinfo.name), lpszSpecifiedDir); // 遞歸實現子目錄查找
					}
				}
			}
			else 
			{
				vctFile.push_back(p.assign(path).append(fileinfo.name));
			}

		} while (_findnext(nFile, &fileinfo) == 0);  // 尋找下一個,成功返回0,否則-1
		_findclose(nFile);
	}
    return TRUE;
}

    在查找到該文件夾後需要刪除,使用DeleteDirectory()函數,考慮該文件中可能有內容,所這裏需要將其內容刪除再移除該文件夾,實現過程

// 刪除文件
bool CDeletDlg::DeleteDirectory(CString DirName)
{
	CString PUBPATH;
	PUBPATH = DirName;
	CFileFind tempFind;
	DirName += "\\*.*";
	BOOL IsFinded = (BOOL)tempFind.FindFile(DirName);
	//cout << IsFinded <<endl;
	while(IsFinded)
	{
		IsFinded = (BOOL)tempFind.FindNextFile();
		if(!tempFind.IsDots())
		{
			CString strDirName;
			strDirName += PUBPATH;
			strDirName += "\\";
			strDirName += tempFind.GetFileName();
			if(tempFind.IsDirectory())
			{
				DeleteDirectory(strDirName);
			}
			else
			{
				SetFileAttributes(strDirName,FILE_ATTRIBUTE_NORMAL); //去掉文件的系統和隱藏屬性
				DeleteFile(strDirName);
			}
		}
	}
	tempFind.Close();
	if(!RemoveDirectory(PUBPATH))
	{
		return false ;
	}
	return true;
}

    點擊按鈕響應,獲得需要刪除的文件名,獲得.exe的當前目錄,進行刪除處理

void CDeletDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	CString szString;
	editString.GetWindowText(szString);
	CString strexe;
	// 獲得當前執行文件路徑
	::GetModuleFileName(NULL,strexe.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
	int nexe = strexe.GetLength();
	int nLen;
	for( nLen = nexe-1;nLen >= 0; nLen--)
	{
		if(strexe[nLen] == '\\')
			break;
	}
	// 獲得執行文件的目錄
	CString szStringDir = strexe.Left(nLen);
	RemoveSpecifiedDir(szStringDir.GetBuffer(0),szString);
	if (!bExit)
	{
		MessageBox("當前目錄以及子目錄無該文件可刪除","提示框",MB_OKCANCEL);
	}
	else
	{
		MessageBox("succ","提示框",MB_OKCANCEL);
	}
	bExit = false;
}

     至此整個在該.exe目錄下(以及子目錄下)刪除某個文件夾的任務完成,當然你也可以不在.exe目錄下,此時你需要改爲例如:C:\\program,後面再做些小調整即可。

以下是整個工程代碼鏈接:https://download.csdn.net/download/hfuu1504011020/10795000

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章