から修正など
// TODO: proper error handling.
void GetFolders(std::vector<std::wstring>& result, const wchar_t* path, bool recursive)
{
HANDLE hFind;
WIN32_FIND_DATA data;
std::wstring folder(path);
folder += L"\\";
std::wstring mask(folder);
mask += L"*.*";
hFind=FindFirstFile(mask.c_str(),&data);
if(hFind!=INVALID_HANDLE_VALUE)
{
do
{
std::wstring name(folder);
name += data.cFileName;
if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
// I see you don't want FILE_ATTRIBUTE_REPARSE_POINT
&& !(data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT))
{
// Skip . and .. pseudo folders.
if (wcscmp(data.cFileName, L".") != 0 && wcscmp(data.cFileName, L"..") != 0)
{
result.push_back(name);
if (recursive)
// TODO: It would be wise to check for cycles!
GetFolders(result, name.c_str(), recursive);
}
}
} while(FindNextFile(hFind,&data));
}
FindClose(hFind);
}
フォルダ内のフォルダ内のフォルダをスキャンします。
難問は、FindFirstFileとFindNextFileに基づいており、必要に応じてディレクトリを除外しています。スタックオーバーフローとインターネットの両方で、このアプローチの概要を示すbazillionサンプルがあります。
簡単な方法:標準のdirectory_iteratorクラス(サブディレクトリに再帰する必要がある場合はrecursive_directory_iterator)を使用します。解決策はのように簡単です:
for (const auto& entry : directory_iterator(path(L"abc"))) {
if (is_directory(entry.path())) {
// Do something with the entry
visit(entry.path());
}
}
あなたがC++ 17で導入<filesystem>ヘッダファイルを含める必要があります。
注:最新のVisual Studio 2017(15.3.5)を使用していますが、これはまだnamespace std
にはありません。代わりにnamespace std::experimental::filesystem
を参照する必要があります。
.
と..
擬似ディレクトリをフィルタリングする必要がないことを特に注意、。それらはディレクトリイテレータによって返されません。
https://stackoverflow.com/questions/5043403/listing-only-folders-in-directory –
の#includeに、これをサポートしていない現在のDIR –
Xom9ik
窓に誓います。 * fSearchOp *を[FindExSearchLimitToDirectories'に設定すると、[FindFirstFileEx'](https://msdn.microsoft.com/en-us/library/windows/desktop/aa364419(v = vs.85).aspx)できます。 ](https://msdn.microsoft.com/en-us/library/windows/desktop/aa364416(v = vs.85).aspx) - ただし、このフラグは無効です。 – RbMm