Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Creating directory through VC++

Posted on 1999-01-15
5
303 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

765 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