Visual C++/WIN32_FIND_DATA

VC++ WIN32_FIND_DATA 사용 예

Soul-Learner 2013. 5. 16. 10:16

VC++ 에서 WIN32_FIND_DATA 구조체를 이용하여 폴더 내의 모든 파일이름을 출력하는 예


#include <vector>

using namespace std;


BOOL CChildView::SetImage(void)

{

CMainFrame* mainWnd = (CMainFrame*)AfxGetMainWnd();

HTREEITEM htreeItem =  mainWnd->m_wndTree.GetSelectedItem();

CString strItemPath;

mainWnd->m_wndTree.GetItemPath(strItemPath, htreeItem);



CString imgPath;

imgPath.Format(_T("%s\\*.JPG"), strItemPath); // 특정 폴더내의 JPG 파일만 검색하려는 의도


WIN32_FIND_DATA FindData = {0};


HANDLE hFind = FindFirstFile(imgPath, &FindData);


        vector<CString> vecFname;


if (hFind != INVALID_HANDLE_VALUE)

{

  do

  {

 PTSTR pszFileName = FindData.cFileName;

 

 // TODO: Use pszFileName in some way...

 CString imgFullPath;

 imgFullPath.Format(_T("%s\\%s"), strItemPath, pszFileName);


                  // 각 이미지파일의 경로를 벡터에 저장한다

 vecFname.push_back(imgFullPath);


  } while (FindNextFile(hFind, &FindData));


  FindClose(hFind);

}


// 벡터에 저장된 모든 이미지파일의 경로를 출력한다

for(int i=0;i<vecFname.size();i++)

{

AfxMessageBox(vecFname[i]);

}


return 0;

}