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

Mar 15, 2011 at 10:03pm
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
   }
Mar 15, 2011 at 10:50pm
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.

Mar 15, 2011 at 11:33pm
is that command:
CreateDirectory("C:\\newguy")
or somthing else?
Mar 16, 2011 at 1:03am
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 Mar 16, 2011 at 1:07am
Mar 16, 2011 at 2:11am
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
}
Mar 16, 2011 at 7:29pm
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 Mar 16, 2011 at 7:30pm
Topic archived. No new replies allowed.