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
}