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

In this article we will discuss different ways to check if given path is of file or directory that exists using Boost Filesystem Library & C++17 Filesystem Library.

Both Boost Filesystem library & C++17 Experimental filesystem library provides certain functions that we are going to use here, 

bool exists(const path& p);

In Boost Library’s boost::filesystem namespace and In C++17 std::experimental::filesystem namespace,

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

Both returns true if the given path points to an entity (file or directory) that exists in filesystem. Also, first one throws filesystem_error, where as overload with error_code& throws nothing.

bool is_regular_file(const path& p);

In Boost Library’s boost::filesystem namespace and In C++17 std::experimental::filesystem namespace,

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

Both returns true if the given path points to a regular file. Also, first one throws filesystem_error, where as overload with error_code & throws nothing.

bool is_directory(const path& p);

In Boost Library’s boost::filesystem namespace and In C++17 std::experimental::filesystem namespace,

bool is_directory(const path& p);
bool is_directory(const path& p, system::error_code& ec);

Both returns true if the given path points to a directory. Also, first one throws filesystem_error, where as overload with error_code& throws nothing.

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

Algo is :

  • First convert the given string path to filesystem::path object.
  • Check if given path exists or not using filesystem::exists() API.
  • Check if given path is a regular file using filesystem::is_regular_file() API.

Complete function for is as follows,

/*
	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;
}

For C++17, use the same function , with following header file and namespace i.e.

#include <experimental/filesystem>

namespace filesys = std::experimental::filesystem;

For Boost Library, use the same function , with following header file and namespace i.e.

#include <boost/filesystem.hpp>

namespace filesys = boost::filesystem;

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

Algo is :

  • First convert the given string path to boost::filesystem::path object.
  • Check if given path exists or not using boost::filesystem::exists() API.
  • Check if given path is a directory using boost::filesystem::is_directory() API.

Complete function is as follows,

/*
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;
}

For C++17, use the same function , with following header file and namespace i.e.

#include <experimental/filesystem>

namespace filesys = std::experimental::filesystem;

For Boost Library, use the same function , with following header file and namespace i.e.

#include <boost/filesystem.hpp>

namespace filesys = boost::filesystem;

Complete example with Boost headers is as follows,

 

#include <iostream>
#include <cassert>
#include <boost/filesystem.hpp>
namespace filesys = boost::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 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;
}
int main() {

	std::string filePath = "/home/varun/Study/Sample/Sample.cpp";

	// Try to check file that actually exists
	// It should return true
	bool result = checkIfFIle(filePath);

	assert(result == true);

	// Try to check if directory for file path
	// It should return false, because thats a file.
	result = checkIfDirectory(filePath);

	assert(result == false);

	std::string dirPath = "/home/varun/Study/Sample";

	// Try to check directory that actually exists
	// It should return true
	result = checkIfDirectory(dirPath);

	assert(result == true);

	dirPath = "/home/varun/Study/Sample99";

	// Try to check directory that don't exists
	// It should return false
	result = checkIfDirectory(dirPath);

	assert(result == false);

	// Try to check if file with directory path
	// It should return false
	result = checkIfFIle(dirPath);

	assert(result == false);
	return 0;
}

Compile above example using Boost with following command in linux,

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

Compile above example in C++17 after changing header file name and namespace, with following command in linux,

g++ -std=c++17 example.cpp -lstdc++fs

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top