if (c:\\newguy\\cpp-dir EXIST)

How do I make an if statement that checks if a directory does exist?

1
2
3
4
if (c:\\newguy exist (true))
   {
       //code here
   }
You could use "SetCurrentDirectory(...)": http://msdn.microsoft.com/en-us/library/aa365530(v=VS.85).aspx and test if it failed or not.

Or you could use "CreateDirectory(...): http://msdn.microsoft.com/en-us/library/aa363855(VS.85).aspx and if it fails with ERROR_ALREADY_EXISTS then you know there is a folder there.

is that command:
CreateDirectory("C:\\newguy")
or somthing else?
Use PathFileExists(_T("c:\\mydir")) API exportat din shlwapi.dll. Works with files too.
CreateDirectory() could work, but why bother with that and create useless directories in case the directory does not exists already?
Last edited on
btw way I do Windows Forms C++/CLI Some call it CLR I think. Does the same command work?

And just to clearify the command is:

PathFileExists(_T("c:\\newguy"));
or
1
2
3
4
if (PathFileExists(_T("c:\\newguy")))
{
code here
}
my favorite way in windows api is to call GetFileAttributes. e.g.

1
2
3
4
5
6
7
8
9
10

if (GetFileAttributes(_T("c:\\newguy")) & FILE_ATTRIBUTE_DIRECTORY)
{
     // directory exists
}
else
{
    // handle error
}


List of other attributes here:
http://msdn.microsoft.com/en-us/library/gg258117%28v=VS.85%29.aspx
Last edited on
Topic archived. No new replies allowed.