is_directory () in VS2012 returning false for even directories

1k views Asked by At

I am running through a directory containing multiple sub-directories. I am using recursive_directory_iterator. I have directory asr->collections->(multiple directories and couple of text files)

#include <iostream>
#include <filesystem>
#include <conio.h>

using namespace std::tr2::sys;

int main () {
std::string path_;
std::cout << " Enter the path ";
std::cin >> path_;

auto dir_path = path(path_);

for (auto it = directory_iterator(dir_path); it != directory_iterator(); ++it)
{
    const auto& file = it->path();
    std::cout << " path : " << file << std::endl;

    if (is_directory (status(it->path()))) 
        std::cout << " It is a directory. " << std::endl;
    else 
        std::cout << " It is not a directory. " << std::endl;
}
_getch();
return 0;
}

I know I had posted this earlier. It was a silly mistake, I changed it. But it is still bugy. The issue I am having is the is-directory returns false for everything. Am I using it wrong. I have linked the MSDN URL below.

I installed boost and ran the code. It worked ! Boost Source

#include <iostream>
#include <boost\filesystem.hpp>
#include <conio.h>

using namespace boost::filesystem;

int main () {
std::string path_;
std::cout << " Enter the path ";
std::cin >> path_;

auto dir_path = path(path_);

for (auto it = directory_iterator(dir_path); it != directory_iterator(); ++it)
{
    const auto& file = it->path();
    std::cout << " path : " << file << std::endl;

    if (is_directory (status(it->path()))) 
        std::cout << " It is a directory. " << std::endl;
    else 
        std::cout << " It is not a directory. " << std::endl;
}
_getch();
return 0;
}

http://msdn.microsoft.com/en-us/library/hh874754.aspx

Also, can I use the boost filesystem documentation be used as a tutorial for this, since there is no good documentation as to what is what and how to use it.

2

There are 2 answers

0
ani On BEST ANSWER

Ok, the issue was fixed after I updated the Visual Studio with first major update.

2
Hans Passant On
if (is_directory (status(dir_path)) )

Yes, you are using it wrong. Try testing the file, not dir_path. You already know that dir_path is a directory.