HANDLE hFind; WIN32_FIND_DATAA dataFind; BOOL bMoreFiles = TRUE; CString strFile; //** Find the to first file in the main directory hFind = FindFirstFile(m_strMainDir + "\\*.*", &dataFind);
//** Find the loop until all files have been found while(hFind != INVALID_HANDLE_VALUE && bMoreFiles == TRUE) { //** check a file has been found and not a directory if (dataFind.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) { //** Get the first letter of the file name int nChar = dataFind.cFileName[0]; //** Convert lower case letters to upper case if (islower(nChar)) { nChar -= 32; }
//** if the file name starts with letter then //** subtract 'A' to find the index in to //** the hLetter array, for others use index 26 if (isalpha(nChar)) { nChar -= 'A'; } else { nChar = 26; }
//** Insert the file name in to the tree m_treeFiles.InsertItem(dataFind.cFileName, hLetter[nChar]); }
//** Find the next file inteh main directory bMoreFiles = FindNextFile(hFind, &dataFind); } //** close the file handle FindClose(hFind);此方法不包括文件夹
WIN32_FIND_DATAA dataFind;
BOOL bMoreFiles = TRUE;
CString strFile;
//** Find the to first file in the main directory
hFind = FindFirstFile(m_strMainDir + "\\*.*", &dataFind);
//** Find the loop until all files have been found
while(hFind != INVALID_HANDLE_VALUE && bMoreFiles == TRUE)
{
//** check a file has been found and not a directory
if (dataFind.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
{
//** Get the first letter of the file name
int nChar = dataFind.cFileName[0];
//** Convert lower case letters to upper case
if (islower(nChar))
{
nChar -= 32;
}
//** if the file name starts with letter then
//** subtract 'A' to find the index in to
//** the hLetter array, for others use index 26
if (isalpha(nChar))
{
nChar -= 'A';
}
else
{
nChar = 26;
}
//** Insert the file name in to the tree
m_treeFiles.InsertItem(dataFind.cFileName, hLetter[nChar]);
}
//** Find the next file inteh main directory
bMoreFiles = FindNextFile(hFind, &dataFind);
}
//** close the file handle
FindClose(hFind);此方法不包括文件夹
然后便利一个文件夹中的所有文件(不包括子文件夹),根据文件名的开头字母,加入到树形控件对应的结点中