文章目录
- 遍历删除空文件夹
- 概述
- 笔记
- END
遍历删除空文件夹
概述
在手工整理openssl3.2编译完的源码工程中的文档, 其中有好多空文件夹.
做了一个小工具, 将空文件夹都遍历删除掉. 这样找文档方便一些.
删除后比对了一下, 空文件夹还真不少.
笔记
// EmptyDirRemove.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include "pch.h"
#include "framework.h"
#include "EmptyDirRemove.h"#ifdef _DEBUG
#define new DEBUG_NEW
#endif// 唯一的应用程序对象CWinApp theApp;using namespace std;CString get_moudle_path();
void DoMyTask();
void findDir(CString csDirPathName, CStringArray& csAryDirPathName);
void tryToDeleteEmptyDir(CStringArray& csAryDirPathName, CStringArray& csAryEmptyDirPathName);int main()
{int nRetCode = 0;HMODULE hModule = ::GetModuleHandle(nullptr);if (hModule != nullptr){// 初始化 MFC 并在失败时显示错误if (!AfxWinInit(hModule, nullptr, ::GetCommandLine(), 0)){// TODO: 在此处为应用程序的行为编写代码。wprintf(L"错误: MFC 初始化失败\n");nRetCode = 1;}else{// TODO: 在此处为应用程序的行为编写代码。DoMyTask();}}else{// TODO: 更改错误代码以符合需要wprintf(L"错误: GetModuleHandle 失败\n");nRetCode = 1;}_tprintf(_T("END\n"));system("pause");return nRetCode;
}void DoMyTask()
{// 安全措施// * 不允许在根目录下运行CString csTmp;CStringArray csAryDirPathName;CStringArray csAryEmptyDirPathName;int i = 0;do {// D:\my_dev\my_local_git_prj\soft\EmptyDirRemove\DebugcsTmp = get_moudle_path();if (csTmp.GetLength() <= 3){_tprintf(_T("危险 : 不允许在根目录下运行此程序\r\n"));}i = AfxMessageBox(_T("即将尝试删除本程序同级目录下的空文件夹, 是否继续?"), MB_OKCANCEL);if (IDOK != i){break;}findDir(csTmp, csAryDirPathName);do {_tprintf(_T("try %d\n"), ++i);tryToDeleteEmptyDir(csAryDirPathName, csAryEmptyDirPathName);if (0 == csAryEmptyDirPathName.GetCount()){break;}} while (true);} while (false);
}void tryToDeleteEmptyDir(CStringArray& csAryDirPathName, CStringArray& csAryEmptyDirPathName)
{csAryEmptyDirPathName.RemoveAll();INT_PTR cnt = csAryDirPathName.GetCount();INT_PTR cnt2 = 0;INT_PTR i = 0;INT_PTR j = 0;CString cs;for (i = 0; i < cnt; i++){cs = csAryDirPathName.GetAt(i);/*!* RemoveDirectory有个好特性, 如果目录非空, 是删除失败的* 所以可以用尝试删除的方法来删除文件夹, 不用怕, 放心大胆的搞:P*/if (RemoveDirectory(cs)){_tprintf(_T("remove dir : %s\n"), (LPCTSTR)cs);csAryEmptyDirPathName.Add(cs);}}// 将已经删除的文件夹名称从csAryDirPathName中移除cnt = csAryEmptyDirPathName.GetCount();for (i = 0; i < cnt; i++){cs = csAryEmptyDirPathName.GetAt(i);cnt2 = csAryDirPathName.GetCount();for (j = 0; j < cnt2; j++){if (cs == csAryDirPathName.GetAt(j)){csAryDirPathName.RemoveAt(j);break;}}}}void findDir(CString csDirPathName, CStringArray& csAryDirPathName)
{CFileFind finder;// build a string with wildcardsCString strWildcard(csDirPathName);strWildcard += _T("\\*.*");// start working for filesBOOL bWorking = finder.FindFile(strWildcard);while (bWorking){bWorking = finder.FindNextFile();// skip . and .. files; otherwise, we'd// recur infinitely!if (finder.IsDots())continue;// if it's a directory, recursively search itif (finder.IsDirectory()){CString str = finder.GetFilePath();// TRACE(_T("%s\n"), (LPCTSTR)str);csAryDirPathName.Add(str);findDir(str, csAryDirPathName);}}finder.Close();
}CString get_moudle_path()
{CString szCurPath(_T(""));GetModuleFileName(NULL, szCurPath.GetBuffer(MAX_PATH), MAX_PATH);szCurPath.ReleaseBuffer();return szCurPath.Left(szCurPath.ReverseFind(_T('\\')) + 1);
}