C++: Check if given path is a file or directory using Boost & C++17 FileSystem Library

In this tutorial, we are going to discuss different ways to check if the given path is a file or directory using Boost & C++17 FileSystem Library.

There are certain functions that we are using here for both Boost Filesystem library & C++17 Experimental filesystem library,

bool exists(const path& p);

bool exists(const path& p);
bool exists(const path& p, error_code& ec);

In Boost Library’s ‘boost::filesystem namespace’ and in C++17 ‘std::experimental::filesystem namespace’, both return true if the given path points to a file or directory that exists in the filesystem. Also, the first one throws filesystem_error, whereas overload with error_code & throws nothing.

bool is_regular_file(const path& p);

bool is_regular_file( const path& p );
bool is_regular_file( const path& p, error_code& ec );

bool is_directory(const path& p);

bool is_regular_file( const path& p );
bool is_regular_file( const path& p, error_code& ec );

In all both returns true if the given path points to a file or directory that exists in the filesystem. Furthermore, the first one throws filesystem_error, whereas overload with error_code& throws nothing.

Do Read: 

Check if given path is a file that exists using Boost & C++17 FileSystem Library

For this, we will write an algorithm-

  • First, we will convert the given string path to boost::filesystem::path object
  • After that, we will check if the given path exists or not using boost::filesystem::exists() API.
  • Also, check if given path is a regular file using boost::filesystem::is_directory() API.

For C++17 library we use this-

#include <experimental/filesystem>

namespace filesys = std::experimental::filesystem;

For Boost Library we use this,

#include <boost/filesystem.hpp>
namespace filesys = boost::filesystem;

so here is the complete code:

#include <iostream>
#include <cassert>
#include <experimental/filesystem>
namespace filesys = std::experimental::filesystem;
/*
    Check if given string path is of a file
*/
bool checkIfFIle(std::string filePath)
{
    try {
        // Create a Path object from given path string
        filesys::path pathObj(filePath);
        // Check if path exists and is of a regular file
        if (filesys::exists(pathObj) && filesys::is_regular_file(pathObj))
            return true;
    }
    catch (filesys::filesystem_error & e)
    {
        std::cerr << e.what() << std::endl;
    }
    return false;
}

Check if given path is a Directory that exists using Boost & C++17 FileSystem Library

For this, we will also write an algorithm-

First, we will convert the given string path to boost::filesystem::path object later we will check if given path exists or not using boost::filesystem::exists() API. And finally, we will also check if given path is a directory using boost::filesystem::is_directory() API.

For C++17 librery we use this-

#include <experimental/filesystem>

namespace filesys = std::experimental::filesystem;

For Boost Library we use this,

#include <boost/filesystem.hpp>

namespace filesys = boost::filesystem;

Complete function is as follows,

#include <iostream>
#include <cassert>

#include <boost/filesystem.hpp>
namespace filesys = boost::filesystem;
/*
Check if given string path is of a Directory
*/
bool checkIfDirectory(std::string filePath)
{
    try {
        // Create a Path object from given path string
        filesys::path pathObj(filePath);
        // Check if path exists and is of a directory file
        if (filesys::exists(pathObj) && filesys::is_directory(pathObj))
            return true;
    }
    catch (filesys::filesystem_error & e)
    {
        std::cerr << e.what() << std::endl;
    }
    return false;
}
}

If you are going to use Boost library then compile with the following command in Linux,

g++ -std=c++11 example.cpp -lboost_filesystem -lboost_system

If you are going to use the C++17 library then compile with the following command in Linux,

g++ -std=c++11 example.cpp -lboost_filesystem -lboost_system

Conclusion:

In this article, we have discussed different ways to check if given path is a file or directory using Boost & C++17 FileSystem Library.