How to check if Directory already Exists in MFC(VC++) ?
I am using below code to get current application Path and there i am creating NDSLog folder
so that all my Logfiles should place there , now i want to check the condition if NDSLog folder already exists dont create it .How to do that ?
Thanks.
char strPathName[_MAX_PATH];
::GetModuleFileName(NULL, strPathName, _MAX_PATH);
// The following code will allow you to get the path.
CString newPath(strPathName);
int fpos = newPath.ReverseFind('\\');
if (fpos != -1)
newPath = newPath.Left(fpos+1);
newPath += "NDSLog\\" ;
CreateDirectory(newPath,NULL);
The simplest way to check if a file/directory exists is to use
GetFileAttributes:Note that the function will return
INVALID_FILE_ATTRIBUTESeven if it fails due to some other reason, such as not having permissions to access the file, so you should check the return value ofCreateDirectoryto make sure that it succeeded.Actually, you don’t need to check whether the directory already exists;
CreateDirectorywill set an error code if the directory already exists: