프로그래밍/C++

C++ 파일 탐색

우대비 2023. 10. 17. 19:36
#include <filesystem>
namespace fs = std::filesystem;

filesystem 라이브러리를 이용하여 파일을 탐색.

 

    const fs::path projectPath =
        fs::current_path().parent_path().parent_path();
    const fs::path modelsPath =
        projectPath / "models" / "glTF-Sample-Models-master" / "2.0";

    SearchModelFiles(modelsPath);

std::filesyste::current_path(), parent_path()함수로 경로를 불러오고

파일을 탐색할 경로를 입력해줌

 

void JsonManager::SearchModelFiles(const filesystem::path &directory) {
        for (const auto &entry : fs::directory_iterator(directory)) {

                if (entry.is_directory()) {
                            SearchModelFiles(entry);
        
                } else if (entry.is_regular_file()) {


                        const auto &filePath = entry.path();
                            const auto &fileName = entry.path()
                                                       .parent_path()
                                                       .parent_path()
                                                       .filename();

                        if (filePath.parent_path().filename() == "glTF" 
                                && filePath.extension() == ".gltf") {
                            cout << "##########" << fileName
                                 << "##########" << endl;
                                       
                            std::cout << "Found gltf file : "
                                      << filePath.filename()
                                << endl;
                        }
                        
                        if (filePath.parent_path().filename() == "screenshot" &&
                            filePath.stem() == "screenshot") {
                        
                                std::cout << "Found screenshot file : "
                                      << filePath.filename() << endl;
                        }
                }
        }
}

 

" for (const auto &entry : fs::directory_iterator(directory)) "으로 

경로 내의 모든 파일을 탐색함

 

.extension() 함수로 파일의 형식을 불러오고

.stem() 함수로 파일 이름을 불러옴

 

 

 

LIST

'프로그래밍 > C++' 카테고리의 다른 글

LNK2019, LNK1120 해결법  (0) 2022.12.15
스마트 포인터  (0) 2022.11.10
C++ Lambda  (0) 2022.11.10
전달 참조(보편 참조)와 forward  (0) 2022.11.09
오른값(rvalue)과 이동 대입 연산자  (0) 2022.11.09