Solved

Creating directory through VC++

Posted on 1999-01-15
5
274 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
Comment Utility
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
Comment Utility
ouuchh,
sorry did not read the client - server ....
reject this
0
 

Author Comment

by:amerolla
Comment Utility
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
Comment Utility
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
Comment Utility
// 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now