본문 바로가기

Visual C++/MFC Recursive Invocation

MFC Recursive Invocation example

주어진 디렉토리 경로를 받아서 그 안의 모든 파일과 모든 하위 폴더명을 검사하여 메시지박스에 출력하는 예

하위 폴더 안에 있는 하위 폴더까지 무한히 반복적으로 모두 검사한다


사용법: Recursive(_T("D:\\test\\*"));


BOOL Recursive(LPCTSTR rootPath)

{

   CFileFind finder;


   // build a string with wildcards

   CString strWildcard(rootPath);


   // start working for files

   BOOL bWorking = finder.FindFile(strWildcard);


   while (bWorking)

   {

      bWorking = finder.FindNextFile();


      // skip . and .. files; otherwise, we'd recur infinitely!

      if (finder.IsDots())

         continue;


      // if it's a directory, recursively search it

      if (finder.IsDirectory())

      {

CString str = finder.GetFilePath(); // 선택된 디렉토리의 완전한 경로

 

AfxMessageBox(str);

Recursive(str+"\\*");

      }

 else

 {

CString str = finder.GetFilePath(); // 선택된 파일의 완전한 경로

AfxMessageBox(str);

 }

   }


   finder.Close();

   return 0;

}