#pragma once #include <vector> using std::vector; // CDlgB 对话框 class CDlgB : public CDialogEx { DECLARE_DYNAMIC(CDlgB) public: CDlgB(CWnd* pParent = NULL); // 标准构造函数 virtual ~CDlgB(); // 对话框数据 enum { IDD = IDD_DIALOG2 }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 DECLARE_MESSAGE_MAP() public: CString m_strPath; CString m_strShow; afx_msg void OnBnClickedButton2(); afx_msg void OnBnClickedButton3(); void ErgodicFile(TCHAR szPath[MAX_PATH]); vector<CString> m_vecPath; CString m_Filter; };
// DlgB.cpp : 实现文件 // #include "stdafx.h" #include "FileCleaner.h" #include "DlgB.h" #include "afxdialogex.h" // CDlgB 对话框 IMPLEMENT_DYNAMIC(CDlgB, CDialogEx) CDlgB::CDlgB(CWnd* pParent /*=NULL*/) : CDialogEx(CDlgB::IDD, pParent) , m_strPath(_T("")) , m_strShow(_T("")) , m_Filter(L".txt.cpp.exe") { } CDlgB::~CDlgB() { } void CDlgB::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Text(pDX, IDC_EDIT1, m_strPath); DDX_Text(pDX, IDC_EDIT2, m_strShow); } BEGIN_MESSAGE_MAP(CDlgB, CDialogEx) ON_BN_CLICKED(IDC_BUTTON2, &CDlgB::OnBnClickedButton2) ON_BN_CLICKED(IDC_BUTTON3, &CDlgB::OnBnClickedButton3) END_MESSAGE_MAP() // CDlgB 消息处理程序 void CDlgB::OnBnClickedButton2() { // TODO: 在此添加控件通知处理程序代码 TCHAR strPath[MAX_PATH]; BROWSEINFO bi = {}; bi.hwndOwner = m_hWnd; bi.pszDisplayName = strPath; LPCITEMIDLIST pid = SHBrowseForFolder(&bi); SHGetPathFromIDList(pid, strPath); m_strPath = strPath; UpdateData(FALSE); CoTaskMemFree((LPVOID)pid); } void CDlgB::OnBnClickedButton3() { // TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); ErgodicFile(m_strPath.GetBuffer()); for (DWORD i = 0; i < m_vecPath.size(); i++) { m_strShow += m_vecPath[i]; m_strShow += L"\r\n"; } UpdateData(FALSE); } void CDlgB::ErgodicFile(TCHAR szPath[MAX_PATH]) { CString strPath = szPath; strPath += L"\\*"; WIN32_FIND_DATA fi = {}; HANDLE hFile = FindFirstFile(strPath, &fi); if (hFile == INVALID_HANDLE_VALUE) { return; } do { if (!lstrcmp(fi.cFileName, L"." )|| !lstrcmp(fi.cFileName, L"..")) { continue; } TCHAR *p = PathFindExtension(fi.cFileName); if (-1 == m_Filter.Find(p)) { continue; } if (fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { CString strDIr = szPath; strDIr += L"\\"; strDIr += fi.cFileName; ErgodicFile(strDIr.GetBuffer()); continue; } CString strDIr = szPath; strDIr += L"\\"; strDIr += fi.cFileName; m_vecPath.push_back(strDIr); } while (FindNextFile(hFile,&fi)); }
时间: 2024-10-28 16:23:36