Improved traverse folder and build directory tree

Source: Internet
Author: User
Tags function prototype knowledge base

Just see the VC Knowledge Base 20 period of an article "Traverse folder and build a directory tree." The main purpose is to scan the target folder with a recursive function, but there is also a limit to the number of directory layers in this function.

I wrote another function, the result of which is exactly the same as the previous one, but eliminates the limit of the number of directory layers, the original code and the improved code as follows:

The original function prototypevoid 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 ())
{
If it's a subdirectory, continue to look deeper with recursion.
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 ())
{
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 ();//Off
}

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.