Reputation: 21147
EDIT: I'm using std::tr2::sys not boost's filesytem
I'm working on a simple app that scans directories on the computer and returns statistics about those files. There are two ways that scan can be called: recursively and non-recursively. A path is passed into the scan method along with a flag for whether or not the scan should be done recursively. Given the path folder
the two implementations look something like this:
directory_iterator start( folder ), end;
for( ; start != end; ++start ) {
//Some code in here
// get the path like this:
auto path = start->path();
}
or
recursive_directory_iterator start( folder ), end;
for( ; start != end; ++start ) {
//Some code in here
// get the path like this:
auto path = start->path();
}
The problem I'm encountering is that in the block that uses the regular directory_iterator when I try and grab the path it will return only the file name ie. "myTextFile.txt" where as getting it from the recursive_directory_iterator returns the full path to the file on the system. In each case the value of folder is the same (a full file path as well). Is there a reason the front part of the path is getting chopped off or is there some way I can get the full file path from just the abbreviated one that I'm getting back from directory_iterator?
Upvotes: 1
Views: 3628
Reputation: 52365
Use the complete function:
auto full_path = std::tr2::sys::complete(path, folder);
Another simple way to do it would be be to simply append the filename to the path (tested on MSVC2012 with the <filesystem>
in the std::tr2::sys
namespace):
for (; start != end; ++start) {
auto path = start->path();
auto full_path = folder / path;
}
Upvotes: 1
Reputation: 47784
For full path you need to use start->path().directory_string()
directory_iterator end;
for (directory_iterator start(p);
start != end;
++start)
{
if (is_directory(start->status()))
{
//A directory
}
if (is_regular_file(start->status()))
{
std::cout << start->path().directory_string()<< "\n";
}
}
Play around with following based on your need, refer boost docs for more details, however names their suggest all.
start->path().branch_path();
start->path().directory_string();
start->path().file_string();
start->path().root_path();
start->path().parent_path();
start->path().string();
start->path().basename();
start->path().filename();
start->path().root_name();
start->path().relative_path();
Upvotes: 2