程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> VC >> 關於VC++ >> 改進《遍歷文件夾並建成目錄樹》

改進《遍歷文件夾並建成目錄樹》

編輯:關於VC++

剛剛看到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;
}

本文配套源碼

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved