How to check if directory exist using C++ and winAPI [duplicate]

How do I check whether a directory exists using C++ and windows API?


Here is a simple function which does exactly this :

#include <windows.h>
#include <string>

bool dirExists(const std::string& dirName_in)
{
  DWORD ftyp = GetFileAttributesA(dirName_in.c_str());
  if (ftyp == INVALID_FILE_ATTRIBUTES)
    return false;  //something is wrong with your path!

  if (ftyp & FILE_ATTRIBUTE_DIRECTORY)
    return true;   // this is a directory!

  return false;    // this is not a directory!
}

If linking to the shell Lightweight API (shlwapi.dll) is ok for you, you can use the PathIsDirectory function