Help us understand the problem. What is going on with this article?

ディレクトリ内にある全ファイル/ディレクトリを列挙する

More than 5 years have passed since last update.
#include <iostream>
#include <algorithm>
#include <filesystem> // std::tr2::sys::path etc.

using namespace std;

int main() {
    namespace sys = std::tr2::sys;
    sys::path p("."); // 列挙の起点
    std::for_each(sys::directory_iterator(p), sys::directory_iterator(),
    //  再帰的に走査するならコチラ↓
    //  std::for_each(sys::recursive_directory_iterator(p), sys::recursive_directory_iterator(),
        [](const sys::path& p) {
            if (sys::is_regular_file(p)) { // ファイルなら...
                cout << "file: " << p.filename() << endl;
            } else if (sys::is_directory(p)) { // ディレクトリなら...
                cout << "dir.: " << p.string() << endl;
            }
        });
}

実行例:

file: ConsoleApplication1.vcxproj
file: ConsoleApplication1.vcxproj.filters
file: ConsoleApplication1.vcxproj.vspscc
dir.: ./Debug
dir.: ./Release
file: Source.cpp
file: trial.txt

[追記] ワイド文字版はコチラ

int main() {
    namespace sys = std::tr2::sys;
    sys::wpath p(L"."); // 列挙の起点
    std::for_each(sys::wdirectory_iterator(p), sys::wdirectory_iterator(),
    //  再帰的に走査するならコチラ↓
    //  std::for_each(sys::wrecursive_directory_iterator(p), sys::wrecursive_directory_iterator(),
        [](const sys::wpath& p) {
        if (sys::is_regular_file(p)) { // ファイルなら...
            wcout << L"file: " << p.filename() << endl;
        }
        else if (sys::is_directory(p)) { // ディレクトリなら...
            wcout << L"dir.: " << p.string() << endl;
        }
    });
}
episteme
επιστημη(えぴすてーめー) / Microsoft MVP for Visual C++ 2004- / CodeZine: http://codezine.jp/author/352
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした