Home > Archive > Visual Studio > October 2004 > FindFirstFile / FindFirstFileEx: Too slow to search files or subdirectories
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
FindFirstFile / FindFirstFileEx: Too slow to search files or subdirectories
|
|
|
| I am using Microsoft Visual C++ 6.0 to develop a dialog-based Win32
application.
I would like to show all the files and subdirectories of C drive with a Tree
Control (CTreeCtrl), just like the windows file explorer does. I use
FindFirstFile to seach the files and subdirectories, and then insert them to
the tree control. However, I found the process is very time-consuming: it
takes me more than 30 seconds to finish the whole process. Could anybody
help me to speed it?
Thanks in advance.
John
Sample:
void CFileOpenDlg::EnumDirs(LPCTSTR pszPath, LPCTSTR pszFilter, HTREEITEM
hItemParent)
{
WIN32_FIND_DATA fd;
HANDLE hFind;
BOOL bFind;
HTREEITEM hItem = TVI_LAST;
CString strSearch(pszPath),
strBase(pszPath);
strSearch += pszFilter;
hFind = FindFirstFile(strSearch, &fd);
bFind = (hFind != INVALID_HANDLE_VALUE);
while(bFind)
{
if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
CString strSub(strBase);
hItem = m_treeFolder.InsertItem(fd.cFileName, 2, 3, hItemParent, hItem);
if(hItem)
{
m_treeFolder.SetItemData(hItem, 1);
strSub += fd.cFileName;
strSub += _T("\\");
EnumDirs(strSub, pszFilter, hItem);
}
}
bFind = FindNextFile(hFind, &fd);
}
}
| |
| Chris Tacke, eMVP 2004-10-20, 4:07 pm |
| Visual C++ 600? C: drive? How is this a Windows CE or Pocket PC question
in any way shape or form? Please target your questions top appropriate
groups.
The answer probably lies in the fact that since you aren't expanding every
node in the tree when you load it then you don't need to fully populate it
at load time.
-Chris
"JohnL" <hiliuzhe@hotmail.com> wrote in message
news:OY0lWistEHA.3088@tk2msftngp13.phx.gbl...
>I am using Microsoft Visual C++ 6.0 to develop a dialog-based Win32
> application.
>
> I would like to show all the files and subdirectories of C drive with a
> Tree
> Control (CTreeCtrl), just like the windows file explorer does. I use
> FindFirstFile to seach the files and subdirectories, and then insert them
> to
> the tree control. However, I found the process is very time-consuming: it
> takes me more than 30 seconds to finish the whole process. Could anybody
> help me to speed it?
>
> Thanks in advance.
>
> John
>
> Sample:
>
> void CFileOpenDlg::EnumDirs(LPCTSTR pszPath, LPCTSTR pszFilter, HTREEITEM
> hItemParent)
> {
> WIN32_FIND_DATA fd;
> HANDLE hFind;
> BOOL bFind;
> HTREEITEM hItem = TVI_LAST;
> CString strSearch(pszPath),
> strBase(pszPath);
>
> strSearch += pszFilter;
>
> hFind = FindFirstFile(strSearch, &fd);
> bFind = (hFind != INVALID_HANDLE_VALUE);
>
> while(bFind)
> {
> if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
> {
> CString strSub(strBase);
>
> hItem = m_treeFolder.InsertItem(fd.cFileName, 2, 3, hItemParent, hItem);
>
> if(hItem)
> {
> m_treeFolder.SetItemData(hItem, 1);
>
> strSub += fd.cFileName;
> strSub += _T("\\");
>
> EnumDirs(strSub, pszFilter, hItem);
> }
> }
>
> bFind = FindNextFile(hFind, &fd);
>
> }
> }
>
>
>
>
| |
| Doug Forster 2004-10-20, 4:07 pm |
| Hi John,
First I'll echo Chris comments re appropriate posting. You promised to
improve your behaviour to a recent responder who complained but I haven't
seen any change at all so far.
Regarding your problem. FindFirstFile etc are actually quite fast on either
platform. Your speed problem is probably caused by having the tree control
in a state whereby it redraws each time you add an item. You could try
hiding the control while rebuilding it or try using SetRedraw on the CE
platform or LockWindowUpdate on the desktop.
Cheers
Doug Forster
"JohnL" <hiliuzhe@hotmail.com> wrote in message
news:OY0lWistEHA.3088@tk2msftngp13.phx.gbl...
>I am using Microsoft Visual C++ 6.0 to develop a dialog-based Win32
> application.
>
> I would like to show all the files and subdirectories of C drive with a
> Tree
> Control (CTreeCtrl), just like the windows file explorer does. I use
> FindFirstFile to seach the files and subdirectories, and then insert them
> to
> the tree control. However, I found the process is very time-consuming: it
> takes me more than 30 seconds to finish the whole process. Could anybody
> help me to speed it?
>
> Thanks in advance.
>
> John
>
> Sample:
>
> void CFileOpenDlg::EnumDirs(LPCTSTR pszPath, LPCTSTR pszFilter, HTREEITEM
> hItemParent)
> {
> WIN32_FIND_DATA fd;
> HANDLE hFind;
> BOOL bFind;
> HTREEITEM hItem = TVI_LAST;
> CString strSearch(pszPath),
> strBase(pszPath);
>
> strSearch += pszFilter;
>
> hFind = FindFirstFile(strSearch, &fd);
> bFind = (hFind != INVALID_HANDLE_VALUE);
>
> while(bFind)
> {
> if(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
> {
> CString strSub(strBase);
>
> hItem = m_treeFolder.InsertItem(fd.cFileName, 2, 3, hItemParent, hItem);
>
> if(hItem)
> {
> m_treeFolder.SetItemData(hItem, 1);
>
> strSub += fd.cFileName;
> strSub += _T("\\");
>
> EnumDirs(strSub, pszFilter, hItem);
> }
> }
>
> bFind = FindNextFile(hFind, &fd);
>
> }
> }
>
>
>
>
| |
|
| I recommend against adding items to the tree control as FindFirstFile finds
them.
Instead, create a tree list object, and insert all the entries in that
instead.
Then loop thru your tree list and add to the tree control.
Having a tree list that is "mapped" to the tree control like that also
allows you to store additional data for each entry.
(you basically have the lparam member of each tree control item point to the
tree list entry it corresponds to,
and such entries hold whatever additional data you may need).
As you manipulate the directory, you'll also have to manipulate the
appropriate entries in your tree list.
(e.g. if you delete a folder, you'll have to delete the appropriate entry in
your tree list, as well as the tree control)
It may sound like a lot of work, but once you get everything in place you
have more control over everything and it's easy to expand.
-----
Chris
|
|
|
|
|