分类: C/C++
2008-08-04 16:05:41
刚刚看到VC知识库二十期中的一篇文章《遍历文件夹并建成目录树》。主要是用一个递归函数来扫描目标文件夹,但是这个函数还存在目录层数的限制。
我写了另外一个函数,实现的结果跟上文一模一样,但消除了目录层数的限制, 原代码与改进后的代码如下:
//原来的函数原型
void CFileTreeDlg::BrowseFile(int CallNum, CString strFile) { CallNum++; CFileFind ff; CString szDir = strFile; if(szDir.Right(1) != "\\") szDir += "\\"; szDir += "*.*"; BOOL res = ff.FindFile(szDir); while(res) { res = ff.FindNextFile(); if(ff.IsDirectory() && !ff.IsDots()) { //如果是一个子目录,用递归继续往深一层找 CString strPath = ff.GetFilePath(); CString strTitle = ff.GetFileTitle(); int i =0; switch(CallNum) { case 1: strHTFir = m_FileTree.InsertItem(strTitle,0,0,NULL); break; case 2: strHTSec = m_FileTree.InsertItem(strTitle,0,0,strHTFir); break; case 3: strHtThi = m_FileTree.InsertItem(strTitle,0,0,strHTSec); break; case 4: strHtFor = m_FileTree.InsertItem(strTitle,0,0,strHtThi); break; default: strHtFif = m_FileTree.InsertItem(strTitle,0,0,strHtFor); break; } BrowseFile(CallNum,strPath); } else if(!ff.IsDirectory() && !ff.IsDots()) { //显示当前访问的文件 CString strPath; CString strTitle; strPath = ff.GetFilePath(); strTitle = ff.GetFileTitle(); switch(CallNum) { case 1: strRoot = m_FileTree.InsertItem(strTitle,0,0,NULL); break; case 2: strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHTFir); break; case 3: strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHTSec); break; case 4: strHtEnd = m_FileTree.InsertItem(strTitle,0, 0,strHtThi); break; case 5: strHtEnd = m_FileTree.InsertItem(strTitle,0, 0,strHtFor); break; default: strHtEnd = m_FileTree.InsertItem(strTitle,0, 0,strHtFif); break; } } } ff.Close();//关闭 }//原来的函数调用
BOOL CFileTreeDlg::OnInitDialog() { CDialog::OnInitDialog(); SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon m_iImageList.Create(24, 24, TRUE,1, 0); HICON hIcon = NULL; hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_KEBIAO), IMAGE_ICON, 24, 24, 0); m_iImageList.Add(hIcon); m_FileTree.SetImageList ( &m_iImageList,TVSIL_NORMAL ); BrowseFile(0,"成绩表"); //遍历"成绩表"文件夹内的所有目录 return TRUE; }//改过后的函数原型
void CFileTreeDlg::BrowseDir( CString strDir, HTREEITEM parent ) { CFileFind ff; CString szDir = strDir; HTREEITEM hSubItem; if(szDir.Right(1) != "\\") szDir += "\\"; szDir += "*.*"; BOOL res = ff.FindFile(szDir); while( res ) { res = ff.FindNextFile(); if(ff.IsDirectory() && !ff.IsDots()) { CString strPath = ff.GetFilePath(); CString strTitle = ff.GetFileTitle(); hSubItem = m_FileTree.InsertItem( strTitle, 0, 0,parent ); BrowseDir( strPath, hSubItem ); } else if(!ff.IsDirectory() && !ff.IsDots()) { CString strTitle = ff.GetFileTitle(); m_FileTree.InsertItem( strTitle, 0, 0, parent ); } } ff.Close(); }//改过后的函数调用
BOOL CFileTreeDlg::OnInitDialog() { CDialog::OnInitDialog(); SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon m_iImageList.Create(24, 24, TRUE,1, 0); HICON hIcon = NULL; hIcon = (HICON)::LoadImage(::AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_KEBIAO), IMAGE_ICON, 24, 24, 0); m_iImageList.Add(hIcon); m_FileTree.SetImageList ( &m_iImageList,TVSIL_NORMAL ); BrowseDir( "成绩表", NULL ); return TRUE; }(全文完)