본문 바로가기

Test Code/C++

[WinApi] 해당 경로의 파일 리스트 조회

/*

GetFiles 함수

해당 경로의 파일 리스트를 가져온다.

bAllDirectories = true 일경우 하위폴더의 파일 리스트도 모두 가져온다.

*/


#include <windows.h>

#include <string>

#include <iostream>

#include <vector>


using namespace std;

void GetFiles(vector<string> &vList, string sPath, bool bAllDirectories);


int main()

{

vector<string> vec;

GetFiles(vec,"C:\\Users",true);

for (auto s:vec)

{

cout << s << endl;

}

return 0;

}


void GetFiles(vector<string> &vList, string sPath, bool bAllDirectories)

{

string sTmp = sPath + string("\\*.*");


WIN32_FIND_DATA fd;

HANDLE hFind = FindFirstFile(sTmp.c_str(), &fd);

if (INVALID_HANDLE_VALUE != hFind)

{

do

{

if ( fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)

{

if (bAllDirectories)

{

if (fd.cFileName[0] != '.')

{

sTmp = 

sPath + 

string("\\") + 

string(fd.cFileName);

GetFiles(vList, sTmp, bAllDirectories);

}

}

}

else

{

sTmp = 

sPath + 

string("\\") + 

string(fd.cFileName);

vList.push_back(sTmp);

}

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

FindClose(hFind);

}

}