📄 flefd.cpp
字号:
// FleFd.cpp : implementation file
//
#include "stdafx.h"
#include "Chapter14.h"
#include "FleFd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFleFd dialog
CFleFd::CFleFd(CWnd* pParent /*=NULL*/)
: CDialog(CFleFd::IDD, pParent)
{
//{{AFX_DATA_INIT(CFleFd)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CFleFd::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFleFd)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFleFd, CDialog)
//{{AFX_MSG_MAP(CFleFd)
ON_BN_CLICKED(IDC_dir_sel, Ondirsel)
ON_BN_CLICKED(IDC_start, Onstart)
ON_BN_CLICKED(IDC_SAVE_FILE, OnSaveFile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFleFd message handlers
void CFleFd::Ondirsel()
{
// TODO: Add your control notification handler code here
LPBROWSEINFO lpbi=new BROWSEINFO;
lpbi->hwndOwner=GetSafeHwnd();//NULL;
lpbi->pidlRoot=NULL;
lpbi->pszDisplayName=NULL;
lpbi->lpszTitle="请选择要遍历的目录位置:";
lpbi->ulFlags=BIF_RETURNONLYFSDIRS|BIF_STATUSTEXT;
lpbi->lpfn=NULL;
//显示外壳文件夹以便用户选择
LPITEMIDLIST lpitemidlist=SHBrowseForFolder(lpbi);
if(lpitemidlist==NULL)
{
delete lpbi;
lpbi = NULL;
return;
}
char path[MAX_PATH];
//转换项目标志府列表为一个系统文件路径
SHGetPathFromIDList(lpitemidlist,path);
delete lpbi;
//获取控件指针
CWnd* pWnd=(CWnd*)GetDlgItem(IDC_dir);
//设置其标题为当前路径信息
pWnd->SetWindowText(path);
//更新对话框
UpdateData(FALSE);
}
void CFleFd::BrowseDir(CString& strDir)
{
//定义查找文件对象
CFileFind cff;
CString szDir = strDir;
CString str;
//当为根目录时,最右侧为'\'
if(szDir.Right(1) != "\\")
szDir += "\\";
//所有文件
szDir += "*.*";
BOOL bResult = cff.FindFile(szDir);
while(bResult)
{
bResult = cff.FindNextFile();
if(cff.IsDirectory() && !cff.IsDots())
{
//如果是一个子目录,用递归继续往深一层找
CString strPath=cff.GetFilePath();
BrowseDir(strPath);
//目录数目自加
ldirCount++;
}
else if(!cff.IsDirectory() && !cff.IsDots())
{
//显示当前访问的文件
CWnd* pWnd = (CWnd*)GetDlgItem(IDC_dir);
str.Format("%s",cff.GetFilePath());
pWnd->SetWindowText(str);
//写到文件
ExportFile.WriteString(str+"\n");
//文件数目自加
lfileCount++;
//为了能看的清楚,可以停留几毫秒
// Sleep(10);
}
//置空
str="";
}
cff.Close();//关闭
}
void CFleFd::Onstart()
{
// TODO: Add your control notification handler code here
CWnd* pWnd=(CWnd*)GetDlgItem(IDC_dir);
CString path,strHint;
pWnd->GetWindowText(path);
//路径信息不能为空
if(path.IsEmpty())
{
AfxMessageBox("目录不能为空");
return;
}
//将文件路径保存为文件
ExportFile.Open(strDirFile,CFile::modeCreate|CFile::modeWrite,NULL);
//计数器复位
lfileCount=0;
ldirCount=0;
BrowseDir(path);
//查找完毕提示
strHint.Format("共有文件夹%d 文件%d个",ldirCount,lfileCount);
AfxMessageBox(strHint);
//关闭文件
ExportFile.Close();
}
void CFleFd::OnSaveFile()
{
// TODO: Add your control notification handler code here
CString strFilter;
strFilter="Txt Files(*.txt)|*.txt||";
CFileDialog dlg( TRUE, NULL, NULL, OFN_EXPLORER|OFN_HIDEREADONLY|
OFN_ENABLESIZING|OFN_FILEMUSTEXIST,strFilter);
dlg.m_ofn.lStructSize = sizeof(OPENFILENAME);
if(dlg.DoModal() == IDOK )
{
strDirFile=dlg.GetPathName();
}
UpdateData(FALSE);
}
BOOL CFleFd::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
//初始化计数器
lfileCount=0;
ldirCount=0;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -