Paul Jurczak
Paul Jurczak

Reputation: 8165

How to obtain an absolute path from directory_iterator in Visual Studio 2012 <filesystem>

I'm trying to obtain an absolute path from directory_iterator in Visual Studio 2012 . It was possible in earlier versions of Boost library with native_file_string(). None of the function members available now returns absolute path. I ended up having to combine directory path with file name:

string         filePath = "C:\\Eigen\\CMakeLists.txt";
string         prefix   = path (filePath).parent_path().string() + "/";
vector<string> fullPaths;  // Full paths of all files in directory.

for (auto i = directory_iterator (path (filePath).branch_path()); i != directory_iterator(); ++i) 
    fullPaths.push_back (prefix + i->path().string());

Is there a way to have a directory iterator capable of returning the full path? In above loop, I tried:

auto s1 = i->path().branch_path();
auto s2 = i->path().directory_string();
auto s4 = i->path().file_string();
auto s5 = i->path().root_path();
auto s6 = i->path().parent_path();
auto s7 = i->path().string();
auto s8 = i->path().basename();
auto s9 = i->path().filename();
auto sa = i->path().root_name();
auto sb = i->path().relative_path();

and none of them returns full path.

EDIT: I noticed that using recursive_directory_iterator instead of directory_iterator will produce full (absolute) path. Is this by design? What if I don't want recursive iteration?

Upvotes: 2

Views: 2132

Answers (1)

Oberon
Oberon

Reputation: 3249

Your are probably looking for absolute(), which is a non-member function.

Upvotes: 3

Related Questions