Traverse folder and build directory tree

Source: Internet
Author: User
Tags bool

Some days ago, I am in the process of doing a program encountered such a problem, to use the tree-type control to quickly browse the files in the specified folder, after some trouble, finally made out, think some of my fellow may encounter and I the same difficulties, so I write the practice, I hope that some help!

(This program runs the effect chart)

The basic principle is to use the recursive call of the function, and then add the parameter tracking. While I was debugging the program, I tracked the discovery that the program returned a directory of the same sibling only after traversing all subdirectories in a directory. This allows you to use the value of the parameter to determine the level of the directory.

(1) Add a tree control to the dialog box and select has Buttons, has Lines, Lines at Root in the style.

(2) Defining variables:

HTREEITEM strRoot; //根目录
HTREEITEM strHTFir; //第一层
HTREEITEM strHTSec; //第二层
HTREEITEM strHtThi; //第三层
HTREEITEM strHtFor; //第四层
HTREEITEM strHtFif; //第五层
// 没有找到更好的方法,所以只能这样来显示,估计一下大约能用到几层,如 果找到好方法,请告诉我,非常感谢!
HTREEITEM strHtEnd; //文件目录
(3) Adding a function to traverse a folder browsefile (int callnum, CString strfile)

Calnum-is used to record the first layers of directories, strfile-paths

and add the following:

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 ())//directory is a folder
{
If it's a subdirectory, continue to look deeper with recursion.
CString strpath = ff. GetFilePath (); Get the path as the beginning of the recursive call
CString strtitle = ff. GetFileTitle (); Get the directory name as a tree-controlled node.
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); Recursive call
}
else if (!ff. Isdirectory () &&!ff. Isdots ())//reach the lowest level of the file
{
Displays the files currently accessed
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 (); Shut down
}
(4) Set the tree control's icon in the OnInitDialog () function and invoke the Custom function Browsefile ()BOOL CFileTreeDlg::OnInitDialog()
{
  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,"课程表"); //遍历"课程表"文件夹 内的所有目录
}

This article supporting source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.