mrDarker
2025-08-22 f8ad0695ff2431cb90640be52b523d6434bdbf83
Common_Class/AutoFileCleanupTool/AutoFileCleanupToolDlg.cpp
@@ -9,6 +9,9 @@
#include "afxdialogex.h"
#include "Config.h"
#include <string>
#include <functional>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
@@ -82,6 +85,88 @@
   , m_bDeleteEmptyFolders(FALSE)        // 默认不删除空文件夹
{
   m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CAutoFileCleanupToolDlg::PerformCleanupTask()
{
   // 遍历源目录
   CFileFind finder;
   CString strSearchPath = m_strSourceFolder + _T("\\*.*");
   if (!finder.FindFile(strSearchPath)) {
      AppendLogLineRichStyled(_T("Source folder not found or empty."), LOG_COLOR_WARNING);
      return;
   }
   std::function<void(const CString&)> ProcessFolder;
   ProcessFolder = [&](const CString& strFolderPath) {
      CFileFind subFinder;
      BOOL bWorking = subFinder.FindFile(strFolderPath + _T("\\*.*"));
      while (bWorking) {
         bWorking = subFinder.FindNextFile();
         if (subFinder.IsDots()) {
            continue;
         }
         CString srcPath = subFinder.GetFilePath();
         CString relPath = srcPath.Mid(m_strSourceFolder.GetLength() + 1);
         CString dstPath = m_strTargetFolder + _T("\\") + relPath;
         if (subFinder.IsDirectory()) {
            ProcessFolder(srcPath);  // 递归处理子文件夹
            continue;
         }
         // 比较文件
         CFileStatus srcStatus, dstStatus;
         if (CFile::GetStatus(srcPath, srcStatus) && CFile::GetStatus(dstPath, dstStatus)) {
            if (srcStatus.m_size == dstStatus.m_size) {
               if (m_bSafeMode) {
                  AppendLogLineRichStyled(_T("Simulated delete: ") + srcPath, LOG_COLOR_WARNING);
               }
               else {
                  CFile::Remove(srcPath);
                  if (PathFileExists(srcPath)) {
                     AppendLogLineRichStyled(_T("Failed to delete: ") + srcPath, LOG_COLOR_ERROR);
                  }
                  else {
                     AppendLogLineRichStyled(_T("Deleted: ") + srcPath, LOG_COLOR_SUCCESS);
                  }
               }
            }
         }
      }
      subFinder.Close();
      // 删除空文件夹(可选)
      if (m_bDeleteEmptyFolders) {
         BOOL bFound = finder.FindFile(strFolderPath + _T("\\*.*"));
         BOOL bEmpty = TRUE;
         while (bFound) {
            bFound = finder.FindNextFile();
            if (finder.IsDots()) {
               continue;
            }
            // 有文件或文件夹,不是空的
            bEmpty = FALSE;
            break;
         }
         finder.Close();
         if (bEmpty) {
            if (RemoveDirectory(strFolderPath)) {
               AppendLogLineRichStyled(_T("Deleted empty folder: ") + strFolderPath, LOG_COLOR_SUCCESS);
            }
            else {
               AppendLogLineRichStyled(_T("Failed to delete empty folder: ") + strFolderPath, LOG_COLOR_ERROR);
            }
         }
      }
   };
   ProcessFolder(m_strSourceFolder);
}
void CAutoFileCleanupToolDlg::SaveSettings()
@@ -458,12 +543,7 @@
      // 清理逻辑放这里
      AppendLogLineRichStyled(_T("Performing auto cleanup..."), LOG_COLOR_NORMAL);
      if (m_bSafeMode) {
         AppendLogLineRichStyled(_T("Safe mode: No files will be deleted."), LOG_COLOR_NORMAL);
      }
      else {
         AppendLogLineRichStyled(_T("Scanning and cleaning files..."), LOG_COLOR_NORMAL);
      }
      PerformCleanupTask();
      AppendLogLineRichStyled(_T("Cleanup cycle completed."), LOG_COLOR_SUCCESS);
   }