The above examples does not use [_access]1 which can be used for both Windows and Linux.
Code sample (Windows) to test with [stat]2, _access() and latest filesystem's [exists]3.
#include <iostream>
#include <Windows.h>
#include <io.h>
#include <filesystem>
#include <vector>
#include <string>
using namespace std;
namespace fs = std::filesystem;
std::vector<std::string> DirPaths
{
"G:\\My-Shcool-2021-22",
"\\\\192.168.111.8\\Oaco\\RotData\\VV-VA",
"\\\\192.168.111.15\\5500\\C-drive\\Oaco\\RotateEarthProject", "\\\\192.168.111.18\\d$\\Mercurial\\Workspace\\HideMoon\\Win32\\Debug",
"Z:\\SuperSaver" //mapped network drive; symbolic link
};
/* test if the path is a directory
*/
void TestDirExists ()
{
int erno =-1;
struct stat info {};
auto ErrorMsg = [&](std::string path)
{
_get_errno(&erno);
if (erno == EACCES)
cout << "access denied " << path << endl;
else if (erno == ENOENT)
cout << "dir path not found " << path << endl;
else if (erno == EINVAL)
cout << "invalid parameter " << path << endl;
};
for (const auto &dp : DirPaths)
{
erno = -1;
if (stat(dp.c_str(), &info) != 0)
ErrorMsg(dp);
if (_access(dp.c_str(), 0) != 0)
ErrorMsg(dp);
if (fs::exists(dp)==0)
ErrorMsg(dp);
if(erno < 0)
cout << "#Dir Found: " << dp << endl;
}
}
[1]: https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/access-waccess?view=msvc-170
[2]: https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions?view=msvc-170
[3]: https://en.cppreference.com/w/cpp/filesystem/exists