[C++] 202 Visual C++におけるglob関数の代替方法

[Windows11, Visual C++17]

Visual C++ではPOSIX固有のglob関数が使えないため、std::filesystem::directory_iteratorを使用します。

ソースコード1つなのでコマンドプロンプトでコンパイル&ビルドしました。

#include <iostream>
#include <string>
#include <vector>
#include <filesystem>

int main()
{
	std::string dir_name = "D:\\code\\VC++\\mylib_vc\\src";
	std::vector<std::string> files;
	std::filesystem::path dir_path(dir_name);

	if (!std::filesystem::is_directory(dir_path)) {
		std::cout << "ディレクトリではありません" << std::endl;
		return -1;
	}
	auto dir = std::filesystem::directory_iterator(dir_path);
	for (auto& p : dir) {
		std::string pathname = p.path().string();
		std::cout << p.path().string() << std::endl;
		files.push_back(pathname); // リスト化して利用
	}
}
cl /std:c++17 /I"D:\code\VC++\mylib_vc\include" /I"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include" ^
/I"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\atlmfc\include" ^
/I"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\VS\include" ^
/I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\ucrt" ^
/I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\um" ^
/I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\shared" ^
/I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\winrt" ^
/I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\cppwinrt" ^
/I"C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\Include\um" ^
"D:\code\VC++\test\test02\FindFile.cpp" /EHsc /link /LIBPATH:"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\ATLMFC\lib\x64" /LIBPATH:"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\lib\x64" /LIBPATH:"C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\lib\um\x64" /LIBPATH:"C:\Program Files (x86)\Windows Kits\10\lib\10.0.22000.0\ucrt\x64" /LIBPATH:"C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22000.0\\um\x64" 
Microsoft(R) C/C++ Optimizing Compiler Version 19.30.30706 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

FindFile.cpp
Microsoft (R) Incremental Linker Version 14.30.30706.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:FindFile.exe
"/LIBPATH:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\ATLMFC\lib\x64"
"/LIBPATH:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\lib\x64"
"/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\lib\um\x64"
"/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.22000.0\ucrt\x64"
"/LIBPATH:C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22000.0\\um\x64"
FindFile.obj
> FindFile.exe
D:\code\VC++\mylib_vc\src\.DS_Store
D:\code\VC++\mylib_vc\src\CharExtract.cpp
D:\code\VC++\mylib_vc\src\ColorConvert.cpp
D:\code\VC++\mylib_vc\src\csvProcessChar.cpp
D:\code\VC++\mylib_vc\src\csvProcessString.cpp
D:\code\VC++\mylib_vc\src\FindFile.cpp
D:\code\VC++\mylib_vc\src\Funcs.cpp
D:\code\VC++\mylib_vc\src\Split.cpp