Look at the following example directly copied from this page. It uses boost::filesystem
so works on all major systems.
int main(int argc, char* argv[])
{
path p (/* Specify a directory here */);
try
{
if (exists(p)) // does p actually exist?
{
if (is_regular_file(p)) // is p a regular file?
cout << p << " size is " << file_size(p) << '\n';
else if (is_directory(p)) // is p a directory?
{
cout << p << " is a directory containing:\n";
copy(directory_iterator(p), directory_iterator(), // directory_iterator::value_type
ostream_iterator<directory_entry>(cout, "\n")); // is directory_entry, which is
// converted to a path by the
// path stream inserter
}
else
cout << p << " exists, but is neither a regular file nor a directory\n";
}
else
cout << p << " does not exist\n";
}
catch (const filesystem_error& ex)
{
cout << ex.what() << '\n';
}
return 0;
}