Solved

Creating directory through VC++

Posted on 1999-01-15
5
297 Views
Last Modified: 2010-04-02
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
Comment
Question by:amerolla
5 Comments
 
LVL 4

Expert Comment

by:nil_dib
ID: 1182295
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
 
LVL 4

Expert Comment

by:nil_dib
ID: 1182296
ouuchh,
sorry did not read the client - server ....
reject this
0
 

Author Comment

by:amerolla
ID: 1182297
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
 
LVL 2

Expert Comment

by:jrmcg
ID: 1182298
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
 

Accepted Solution

by:
DOMINIC011899 earned 50 total points
ID: 1182299
// 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question