// ClearFilesDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "ClearFiles.h"
#include "ClearFilesDlg.h"
#include "afxdialogex.h"
#include "DlgAddOrDel.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CClearFilesDlg 对话框
CClearFilesDlg::CClearFilesDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CClearFilesDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CClearFilesDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT_ROOT_FOLDER_NAME, m_edRootFolder);
DDX_Control(pDX, IDC_BUTTON_SELECT_FOLDER, m_btnSelectFolder);
DDX_Control(pDX, IDC_LIST1, m_lstFolder);
DDX_Control(pDX, IDC_LIST2, m_lstFile);
DDX_Control(pDX, IDC_CHECK1, m_folderCheck);
DDX_Control(pDX, IDC_CHECK2, m_fileCheck);
}
BEGIN_MESSAGE_MAP(CClearFilesDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDOK, &CClearFilesDlg::OnBnClickedOk)
ON_BN_CLICKED(IDC_BUTTON2, &CClearFilesDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON1, &CClearFilesDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON_ADD_FOLDER, &CClearFilesDlg::OnBnClickedButtonAddFolder)
ON_BN_CLICKED(IDC_BUTTON_DEL_FOLDER, &CClearFilesDlg::OnBnClickedButtonDelFolder)
ON_BN_CLICKED(IDC_BUTTON_ADD_FILE, &CClearFilesDlg::OnBnClickedButtonAddFile)
ON_BN_CLICKED(IDC_BUTTON_DEL_FILE, &CClearFilesDlg::OnBnClickedButtonDelFile)
ON_BN_CLICKED(IDC_BUTTON_SELECT_FOLDER, &CClearFilesDlg::OnBnClickedButtonSelectFolder)
END_MESSAGE_MAP()
// CClearFilesDlg 消息处理程序
BOOL CClearFilesDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
GetDlgItem(IDC_BUTTON1)->ShowWindow(SW_HIDE);
GetDlgItem(IDC_BUTTON2)->ShowWindow(SW_HIDE);
m_lstFolder.AddString(_T("Debug"));
m_lstFolder.AddString(_T("debug"));
m_lstFolder.AddString(_T("Release"));
m_lstFolder.AddString(_T("release"));
m_lstFolder.AddString(_T("ipch"));
m_lstFile.AddString(_T("txt"));
m_lstFile.AddString(_T("log"));
m_lstFile.AddString(_T("tlog"));
m_lstFile.AddString(_T("ipch"));
m_lstFile.AddString(_T("aps"));
m_lstFile.AddString(_T("plg"));
m_lstFile.AddString(_T("npt"));
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CClearFilesDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CClearFilesDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CClearFilesDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CClearFilesDlg::OnBnClickedOk()
{
CString cstrTemp, cstrFolder, cstrFile;
m_edRootFolder.GetWindowText(cstrFolder);
int nFolderCount = m_lstFolder.GetCount();
int nFileCount = m_lstFile.GetCount();
if (BST_CHECKED == m_folderCheck.GetCheck())
{
for (int iFolder = 0; iFolder < nFolderCount; iFolder++)
{
m_lstFolder.GetText(iFolder, cstrTemp);
DeepDeleteFoldersByName(cstrFolder, cstrTemp);
}
}
else if (BST_UNCHECKED == m_folderCheck.GetCheck())
{
for (int iFolder = 0; iFolder < nFolderCount; iFolder++)
{
m_lstFolder.GetText(iFolder, cstrTemp);
DeleteFoldersByName(cstrFolder, cstrTemp);
}
}
if (BST_CHECKED == m_fileCheck.GetCheck())
{
for (int iFile = 0; iFile < nFileCount; iFile ++)
{
m_lstFile.GetText(iFile, cstrTemp);
DeepDeleteFilesBykind(cstrFolder, cstrTemp);
}
}
else if (BST_UNCHECKED == m_fileCheck.GetCheck())
{
for (int iFile = 0; iFile < nFileCount; iFile ++)
{
m_lstFile.GetText(iFile, cstrTemp);
DeleteFilesBykind(cstrFolder, cstrTemp);
}
}
// TODO: 在此添加控件通知处理程序代码
// CFile file;
// TCHAR chFilePathName[MAX_PATH] = { 0 };
// GetModuleFileName(NULL, chFilePathName, MAX_PATH);
// CString cstrFilePath, cstrFilePathName, cstrDeleteFilePathName;
// cstrFilePath.Format(_T("%s"), chFilePathName);
// UINT nPos = cstrFilePath.ReverseFind(_T('\\'));
// cstrFilePath = cstrFilePath.Left(nPos + 1);
// cstrFilePathName = cstrFilePath + _T("*.log");
//
// // 删除一个文件夹下一类文件
// CFileFind fileFinder;
// BOOL ret = fileFinder.FindFile(cstrFilePathName);
// while (ret)
// {
// ret = fileFinder.FindNextFile(); // 遍历里面的每一个文件
// cstrDeleteFilePathName = cstrFilePath + fileFinder.GetFileName();
// BOOL bRet = ::DeleteFile(cstrDeleteFilePathName.GetBuffer(cstrDeleteFilePathName.GetLength()));
// cstrDeleteFilePathName.ReleaseBuffer();
// }
// fileFinder.Close();
}
// 遍历一个文件夹下面的所有文件夹
void CClearFilesDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
// DeepDeleteFilesBykind(_T("E:\\work\\test\\ClearFiles"), _T("tlog"));
DeepDeleteFoldersByName(_T("E:\\work\\test"), _T("ipch"));
}
BOOL CClearFilesDlg::TraverseFolders(CString strFolderPath)
{
CFileFind ff;
strFolderPath += (_T("\\*.*"));
BOOL bfinded = ff.FindFile(strFolderPath);
CString strSubFolder;
while(bfinded)
{
bfinded = ff.FindNextFile();
if (! ff.IsDots() && ff.IsDirectory())
{
strSubFolder = ff.GetFileName();
m_vecFolderPath.push_back(strSubFolder);
}
}
ff.Close();
return TRUE;
}
BOOL CClearFilesDlg::DeepTraverseFolders(CString strFolderPath)
{
CFileFind ff;
CString strSubFolderPathTemp = strFolderPath + _T("\\");
strFolderPath += (_T("\\*.*"));
BOOL bfinded = ff.FindFile(strFolderPath);
CString strSubFolder;
while (bfinded)
{
bfinded = ff.FindNextFile();
if (!ff.IsDots() && ff.IsDirectory())
{
strSubFolder = ff.GetFileName();
strSubFolder = strSubFolderPathTemp + strSubFolder;
m_vecFolderPath.push_back(strSubFolder);
DeepTraverseFolders(strSubFolder);
}
}
ff.Close();
return TRUE;
}
BOOL CClearFilesDlg::DeepDeleteFolderAndIncludeFiles(CString strPath)
{
CFileFind ff;
CString cstrDeleteFilePathName;
BOOL bFinded = ff.FindFile(strPath + _T("\\*.*"));
BOOL bRet = FALSE;
while (bFinded)
{
bFinded = ff