Created
January 21, 2017 07:07
-
-
Save LusainKim/7dbe20039a96d0e9fad15b7ac103e592 to your computer and use it in GitHub Desktop.
콘솔에서 지정한 디렉터리에 위치한 모든 파일의 목록을 반환하는 예제입니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////////////////////////////////////////////////// | |
// Get Files | |
//////////////////////////////////////////////////////////////////////////////////// | |
// summary : 파일 경로의 목록을 vector 형식으로 반환합니다. | |
// remark : 하위 목록이라고 별도의 목록에 담기지 않습니다. | |
// author : @Lusain_Kim | |
//////////////////////////////////////////////////////////////////////////////////// | |
// 유니코드 전용 샘플 코드 | |
#ifndef UNICODE | |
#define UNICODE | |
#endif // !UNICODE | |
#include <Windows.h> | |
#include <iostream> | |
#include <vector> | |
#include <string> | |
#define STR_TXT(T) TEXT(T) ## s | |
#ifdef UNICODE | |
#define tstring wstring | |
#else | |
#define tstring string | |
#endif | |
using namespace std; | |
void GetFiles(vector<tstring> &vList, tstring sPath, bool bSearchAllDirectories, bool bAllDirectories, int length = -1); | |
int main() | |
{ | |
wcout.imbue(locale("ko-kr")); | |
wcin.imbue(locale("ko-kr")); | |
tstring inputPath; | |
bool bSearchAll = true; | |
bool bAllDir = true; | |
char inputYN = 0; | |
vector<tstring> vecPath; | |
wcout << TEXT("검색을 원하는 디렉터리를 입력하세요.") << endl; | |
wcin >> inputPath; | |
do { | |
wcout << TEXT("모든 하위 경로를 조사합니까? y / n") << endl; | |
rewind(stdin); | |
cin >> inputYN; | |
switch (inputYN) | |
{ | |
case 'y': | |
case 'Y': | |
wcout << TEXT("모든 하위경로를 조사합니다.") << endl; | |
bSearchAll = true; | |
break; | |
case 'n': | |
case 'N': | |
bSearchAll = false; | |
wcout << TEXT("현재 경로만 조사합니다.") << endl; | |
break; | |
default: | |
wcout << TEXT("옳지 않은 선택입니다!") << endl; | |
continue; | |
} | |
wcout << TEXT("전체 경로를 표시합니까? y / n") << endl; | |
rewind(stdin); | |
cin >> inputYN; | |
switch (inputYN) | |
{ | |
case 'y': | |
case 'Y': | |
bAllDir = true; | |
wcout << TEXT("전체 경로를 표시합니다.") << endl; | |
goto SearchDir; | |
case 'n': | |
case 'N': | |
bAllDir = false; | |
wcout << TEXT("현재 경로만 표시합니다.") << endl; | |
goto SearchDir; | |
default: | |
wcout << TEXT("옳지 않은 선택입니다!") << endl; | |
continue; | |
} | |
} while (true); | |
SearchDir: | |
GetFiles(vecPath, inputPath.c_str(), bSearchAll, bAllDir); | |
for (auto s : vecPath) | |
{ | |
wcout << s << endl; | |
} | |
system("pause"); | |
return 0; | |
} | |
void GetFiles(vector<tstring> &vList, tstring sPath, bool bSearchAllDirectories, bool bAllDirectories, int length) | |
{ | |
tstring sTmp = sPath + tstring(TEXT("\\*.*")); | |
WIN32_FIND_DATA fd; | |
HANDLE hFind = FindFirstFile(sTmp.c_str(), &fd); | |
if (INVALID_HANDLE_VALUE != hFind) | |
{ | |
do | |
{ | |
if (fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) | |
{ | |
if (bSearchAllDirectories) | |
{ | |
if (fd.cFileName[0] != '.') | |
{ | |
sTmp = sPath | |
+ STR_TXT("\\") | |
+ tstring(fd.cFileName); | |
GetFiles(vList, sTmp, bSearchAllDirectories, bAllDirectories, (length < 0 ? (sPath + STR_TXT("\\")).length() : length)); | |
} | |
} | |
} | |
else | |
{ | |
sTmp = sPath | |
+ STR_TXT("\\") | |
+ tstring(fd.cFileName); | |
if (length < 0) length = sPath.length(); | |
vList.push_back(bAllDirectories ? sTmp : sTmp.substr(length + 1, sTmp.length() - length)); | |
} | |
} while (FindNextFile(hFind, &fd)); | |
FindClose(hFind); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment