[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

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?
0
amerolla
Asked:
amerolla
1 Solution
 
nil_dibCommented:
Test for the directory with GetFileAttributes.
Create it if it doesn't exist.
if( GetFileAttributes("c:\\my_dir") == 0xFFFFFFFF )
  CreateDirectory("C:\\my_dir", NULL);
0
 
nil_dibCommented:
ouuchh,
sorry did not read the client - server ....
reject this
0
 
amerollaAuthor Commented:
As nil_dib wrote in his update:

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

so, I'll do as he says and reject it.
0
 
jrmcgCommented:
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

\\<ComputerName>\<drive>\Path1\Path2

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

CreateDirectory("C:\\Dir1\\Dir2",NULL);

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...
J.R.

0
 
DOMINIC011899Commented:
// 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>


enum { OK, ERR_CANNOT_CREATE_DIR };
int CreateDir( const char *szkDir );



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

      if      ( CreateDir( szCmdLn[1] ) == OK )
            std::cout << "Directory [" << szCmdLn[1] << "] created.\n";
      else
            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] == ':' )
                  continue;

            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;
                        break;
                        }
                  }
            }

      return nResult;
      }



0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now