본문 바로가기

프로그래밍/MFC

하위 디렉터리까지 검색하는 소스(재귀함수)





void FindSubFolder(char *path)
{
    SetCurrentDirectory(path); //현재 검색할 디렉터리 설정.

    CFileFind find;
    CString pname;
    char newpath[MAX_PATH];

    int res = find.FindFile();

    while(res)
    {
        res = find.FindNextFile();

        if(!find.IsDots()) //. ..은 제외하고
        {
            if(find.IsDirectory()) //디렉터리일때

            {
                pname = find.GetFilePath();

                strcpy(newpath, pname);
               
                FindSubFolder(newpath); //재귀함수 호출.
            }

            total_cnt++;
            pname = find.GetFilePath();
            TRACE("[%04d] PATH : %s\n",total_cnt, pname);
        }
    }
}


void CSearchSubFolderDlg::OnBtnSearch()
{
    SetCurrentDirectory("C:\\");
   
    char path[MAX_PATH];

    strcpy(path,
"C:\\G1_DATA\\");

    FindSubFolder(path);

    TRACE("Search sub_folder end! \n");
    TRACE("total_cnt [%d]\n",total_cnt);   
}