Hi,

I have been going crazy trying to come up with a recursive function for populating a CTreeCtrl from a list of paths in the database like shown below:

c:\documents and settings\owner\file.htm
c:\test\test2\file2.htm
c:\test\tset3\file3.htm
c:\documents and settings\owner\testagain\testagain.htm

I am sure someone else has already done this before, so I am hoping someone can post some code. I would be grateful for any help I could receive. Thanks in advance

Here is what I have so far, but it is not working if I try to add more than 3 levels deep and I don't even know if I am on the right track.:

Code:
//add a root item
TV_INSERTSTRUCT TreeCtrlItem; 
   TreeCtrlItem.hParent       = TVI_ROOT; 
   TreeCtrlItem.hInsertAfter  = TVI_LAST; 
   TreeCtrlItem.item.mask     = TVIF_TEXT | TVIF_PARAM; 
   TreeCtrlItem.item.pszText  = _T("c:\\"); 
   TreeCtrlItem.item.lParam   = (long)"c:\\"; 
   HTREEITEM hTreeItem1       = m_Tree.InsertItem(&TreeCtrlItem); 

AddPath("c:\\hello\\test\\"); //this works
//AddPath("c:\\hello\test\\again\\"); //this crashes if uncommented

....

void CTreeViewTestDlg::AddPath(CString cszPath)
{
	HTREEITEM hParent = NULL;
	int nPos = 0;
	CString cszCaption = "";
	CString cszKey = cszPath;

	if(cszPath.Mid(cszPath.GetLength()-1, 1) == "\\")
	{
		cszPath = cszPath.Mid(0, cszPath.GetLength()-1);
	}
	nPos = cszPath.ReverseFind('\\');
	cszCaption = cszPath.Mid(nPos+1, cszPath.GetLength());
	cszPath = cszPath.Mid(0, nPos+1);


	//does the node already exist?
	HTREEITEM bExists = DoesNodeExists(cszPath, m_Tree.GetRootItem());
	if(bExists== NULL)
	{
		//add 
		//AfxMessageBox("Exists");
		AddPath(cszPath);
	}


	bExists = DoesNodeExists(cszPath, m_Tree.GetRootItem());
	
	  TV_INSERTSTRUCT TreeCtrlItem; 


	  TVITEM test; 
	memset(&test, 0, sizeof(TVITEM));
	test.hItem = bExists;
	
	m_Tree.GetItem(&test);
	
	CString csztest;
	csztest.Format("%s", test.lParam);


   TreeCtrlItem.hParent       = bExists; 
   TreeCtrlItem.hInsertAfter  = TVI_LAST; 
   TreeCtrlItem.item.mask     = TVIF_TEXT | TVIF_PARAM; 

   LPSTR caption = (LPSTR)(LPCTSTR)cszCaption; 
   TreeCtrlItem.item.pszText  = caption;

   LPSTR ps = (LPSTR)(LPCTSTR)cszKey; 
   TreeCtrlItem.item.lParam   = (long)ps; 

   HTREEITEM hTreeItem1       = m_Tree.InsertItem(&TreeCtrlItem); 
	

}

HTREEITEM CTreeViewTestDlg::DoesNodeExists(CString cszPath, HTREEITEM hRootItem)
{
	HTREEITEM hTreeItem=NULL;

	//check the root item

	TVITEM TreeCtrlItem2; 
	memset(&TreeCtrlItem2, 0, sizeof(TVITEM));
	TreeCtrlItem2.hItem = hRootItem;
	
	m_Tree.GetItem(&TreeCtrlItem2);
	
	CString cszLParam;
	cszLParam.Format("%s", TreeCtrlItem2.lParam);

	if(cszLParam == cszPath)
		return hRootItem;

	//check rest of tree
	HTREEITEM hItem = m_Tree.GetChildItem(hRootItem); 
    while (NULL != hItem) 
    { 
			TVITEM TreeCtrlItem2;
		memset(&TreeCtrlItem2, 0, sizeof(TVITEM));
		TreeCtrlItem2.hItem = hItem;
		
		m_Tree.GetItem(&TreeCtrlItem2);
		
		CString cszLParam;
		cszLParam.Format("%s", TreeCtrlItem2.lParam);
		
		if(cszLParam == cszPath)
			return hItem;

        DoesNodeExists(cszPath, hItem); 


        // Move on to next sibling 
        hItem = m_Tree.GetNextSiblingItem(hItem); 
    } 



	return hTreeItem;
}