Using C++ std::filesystem to validate a directory

133 Views Asked by At

For the C++ function std::filesystem::is_directory(path), what is the definition of "is a directory"?

For example with std::filesystem::path = p(".\\xyz"), is_directory(p) returns false. The directory xyz does not exist, but the path functions just inspect the path, not the files on disk.

In my situation, user is inputting a string, and I need to check that it is a valid path for the Windows NTFS filesystem, then that it is a valid directory name (under Windows, if it's a valid path it should be a valid directory name), then that the directory already exists. If any of these is not true, then I need to give the user an error message explaining what is wrong.

Windows 11, Visual Studio 2022

Update 1: I want to do my checking using only std::filesystem, not Boost or Microsoft-specific functions. All the cited "duplicates" use functionality outside std::filesystem.

Update 2: I was able to accomplish what I wanted by using the fs::is_directory(fsp, ec) form and checking the error code ec.

0

There are 0 best solutions below