카테고리 없음2008. 7. 7. 01:06
CFtpFileFind 는 재귀 호출시 약간의 주의가 필요하다.
재귀 호출중, 이전으로 돌아갈 때,
다음 파일 또는 폴더에 대해서 CFtpFileFind::FindFile() 을 한번 더 호출해 주어야 한다.
하지만,  CFtpFileFind::FindNextFile() 을 호출하는 파일 탐색 중에는 다음 파일 정보를 미리 알 수 없으므로,
다음과 같이 파일 수집과, 재귀 호출을 분리하여, 두번의 루프를 사용해야 한다.

// 이하, 약식 코드

CFilInfo : user defined file information class...

CInternetSession * m_pInternetSession = new CInternetSession;
CFtpConnection * m_pFTPConnection = (CFtpConnection*) m_pInternetSession->GetFtpConnection(szAddress);
CFtpFileFind * m_pFtpFind = new CFtpFileFind(m_pFTPConnection);

BOOL GetFTPFileListRecursive(CFilInfo *pFileInfo)
{
   CString strPath;
   strPath.Format(_T("%s/*"), pFileInfo->GetRemotePath());
   
   m_pFTPConnection->SetCurrentDirectory(pFileInfo->GetRemotePath());
      
    // gathering child file list first...
   BOOL bContinue = m_pFtpFind->FindFile(strPath);
   while (bContinue)
   {
       bContinue = m_pFtpFind->FindNextFile();       
       ////////
       if (m_pFtpFind->IsDots() == FALSE )
           continue;
      
       if(m_pFtpFind->IsDirectory() == TRUE)
       {   
           // create file information.
           CFileinfo * pFileChild = new CFileInfo;
           pFileChild->SetIsDirectory(TRUE); // mark directory flag.
          
           // do something...
           // ... m_pFtpFind->GetFileName() ...

           // save child file
           pFileInfo->AddChild(pFileChild);

           ////////
           // invalid usage !!!
           // GetFTPFileListRecursive(pFileChild);
           // m_pFTPConnection->SetCurrentDirectory(pFileInfo->GetRemotePath());                       
           ////////
       }
       else
       {           
           // do something...
           // ... pftpFind->GetFileName() ...
       }
   }
  
  // recursive calling here   
   INT_PTR nSize = pFileInfo->GetChildCount();
   for(INT_PTR iCnt = 0; iCnt < nSize; iCnt++)
   {
       CFileInfo * pFileInfoChild = pFileInfo->GetChild(iCnt);
       if( pFileInfoChild == NULL )
           continue;

       if( pFileInfoChild->GetIsDirectory() == FALSE )
           continue;

       GetFTPFileListRecursive(pFileInfoChild);
   }
  
   return TRUE;
}
Posted by 좋은나무