Article View: borland.public.cppbuilder.nativeapi
Article #11435Re: Some advice on finding files and subdirs
From: Remy Lebeau (TeamB)
Date: Tue, 10 Feb 2004 23:56
Date: Tue, 10 Feb 2004 23:56
72 lines
3612 bytes
3612 bytes
"Greg Stantin" <gstantin@stantonassoc.com> wrote in message
news:402966b0$1@newsgroups.borland.com...
> Even though it works okay, it still displays some dirs with a plus
> sign in the tree, as thought there are files in that dir. But when i
> click on the plus sign, there is nothing there.
Part of the problem is that you are not taking "." and ".." into account
when you are assigning the HasChildren property. Remember that *all*
folders have those two entries present, so they always show up when you call
FindFirstFile(). You should move that code into its own function that
checks for actual file/folder entries being present, ie:
void TForm1::BuildTreeView(TTreeNode* ANode, const AnsiString APath)
{
//...
Node->HasChildren = FolderHasChildren(APath + Data.cFileName);
//...
}
bool __fastcall FolderHasChildren(const AnsiString &APath)
{
bool bHasChildren = false;
AnsiString Path = IncludeTrailingBackslash(APath);
WIN32_FIND_DATA Data;
HANDLE FileHandle = FindFirstFile(Path + "*.*").c_str(), &Data);
if( FileHandle != INVALID_HANDLE_VALUE )
{
do {
bHasChildren = ( !AnsiSameText(Data.cFileName, ".") &&
!AnsiSameText(Data.cFileName, "..") );
}
while( (!bHasChildren) && (FindNextFile(FileHandle, &Data)) );
FileClose(FileHandle);
}
return bHasChildren;
}
Alternative, you could just leave HasChildren = false for new nodes, and
then set it to true when you actually add a new child to it:
void TForm1::BuildTreeView(TTreeNode* ANode, const AnsiString APath)
{
//...
if( ANode != NULL )
{
Node = TreeView1->Items->AddChild(ANode, Data.cFileName);
ANode->HasChildren = true;
}
else
{
Node = TreeView1->Items->Add(NULL, Data.cFileName);
Node->HasChildren = false;
}
//...
}
> I double check in explorer, and there is nothing there.
The thing about Explorer, though, is that it uses a *completely* different
mechanism for tracking and displaying folders and files. The alternative to
the above is to use the same mechanism that Explorer uses - use the
IShellFolder interface for everything instead of iterating the file system
manually. IShellFolder() has a GetAttributesOf() method that can return
SFGAO_FILESYSTEM, SFGAO_FOLDER, and SFGAO_HASSUBFOLDER attributes in a
single call (amongst many others), and an EnumObjects() method for
enumerating the sub-items (files and subfolders).
Gambit
Technical Details
Message-ID:
<40296dfc$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<402966b0$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,fcdbc28037201e15 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2004-02-10 16:08:35 PST From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <402966b0$1@newsgroups.borland.com> Subject: Re: Some advice on finding files and subdirs Date: Tue, 10 Feb 2004 15:56:49 -0800 Lines: 74 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 67.22.138.253 Message-ID: <40296dfc$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1076456956 67.22.138.253 (10 Feb 2004 15:49:16 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail