Creating directory through VC++

I have a server based application that needs to check for the existence of a directory, the full path name of which is provided by the client. If the directory does not exist, it will create it. Any ideas on how to do this?
Who is Participating?
DOMINIC011899Connect With a Mentor Commented:
// Amerolla, here is VC++ code that creates a directory
// through the function CreateDir.  Due to the tab characters
// in the code, it may not look good in this answer window
// but cut and paste it in your VC++ environment and you will
// read it better.
// It was created as a console program for simplicity
// and testing purposes.

#include <windows.h>
#include <IOSTREAM>
#include <STRSTREAM>

int CreateDir( const char *szkDir );

void main( int nCnt, char *szCmdLn[] )
      if      ( --nCnt < 1 )
            std::cout << "USAGE: CreateDir <dirpath>" << std::endl;

      if      ( CreateDir( szCmdLn[1] ) == OK )
            std::cout << "Directory [" << szCmdLn[1] << "] created.\n";
            std::cout << "Could not create directory [" << szCmdLn[1] << "].\n";


int CreateDir( const char *szkDir )
      int nResult = OK;

      //      Create one path at a time
      std::istrstream isDir( szkDir );
      if      ( !isDir )
            return ERR_CANNOT_CREATE_DIR;

      //      A buffer use in reading an input stream
      char szBufLine[MAX_PATH];
      //      A buffer for assembling the path, directory by directory
      char szPath[MAX_PATH];
      memset( szPath, NULL, MAX_PATH );      //      Initialize szPath

      //      Get the text delimited by backslashes
      while ( isDir.getline( szBufLine, MAX_PATH, '\\' ) )
            //      Append or assemble the dir names to szPath one by one
            //      Use a safe concatenation (so as not to overwrite memory)
            int nLen = strlen( szBufLine );
            strncat( szBufLine, "\\", (MAX_PATH-1)-nLen );
            nLen = strlen( szPath );
            strncat( szPath, szBufLine, (MAX_PATH-1)-nLen );

            //      If a drive name, don't call CreateDirectory()
            if      ( szBufLine[strlen( szBufLine )-2] == ':' )

            BOOL bResult = CreateDirectory( szPath, NULL );

            //      If cannot create directory
            if      ( !bResult )
                  //      Get the directory error
                  DWORD dwLastError = GetLastError();
                  //      If the error is other than it is already existing
                  if      ( dwLastError != ERROR_ALREADY_EXISTS )
                        nResult = ERR_CANNOT_CREATE_DIR;

      return nResult;

Test for the directory with GetFileAttributes.
Create it if it doesn't exist.
if( GetFileAttributes("c:\\my_dir") == 0xFFFFFFFF )
  CreateDirectory("C:\\my_dir", NULL);
sorry did not read the client - server ....
reject this
amerollaAuthor Commented:
As nil_dib wrote in his update:

       sorry did not read the client - server ....
       reject this"

so, I'll do as he says and reject it.
Assuming you are running on two separate computers networked together --

what nil_dib wrote was actually correct to an extent.

The path you will check for is the full path that the client has provided with an addition to the beginning of the path.

if the client specifies C:\Dir1\Dir2

then the path you would search for from the server is


the 2 "\\" are necessary at the beginning so the string equivalent would be "\\\\"

<ComputerName> is the name of the computer as seen by the Network.
<drive>                 is the name of the directory as seen by the Network
                            if it is just shared as "d"  then \d\ is all that is necessary.
the Paths are the same.  So you see you may have to do some alterations to the strings you are using but following nil_dib's instructions along with these should do the trick.

Now if you are on the same computer then doing exactly what nil_dib said will work.

One thing to think about is that each directory must exist in order to create it.

in other words


will not work if there is no Dir1. So you would have to CreateDirectory("C:\\Dir1",NULL)
and so on....

if there is a way around this, I don't know it - but there may be one.

If you look at the GetFileAttributes and CreateDirectory in the on-line help, you can see how these functions work.

If you have any other questions I'll help.  I didn't post this as an answer because what nil_dib said was the correct way to go about it.  This is how I do it anyway.

Somebody else may know another, and if they do it would probably help me out too.

Let me know if you need any other help...

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.