Solved

Create a Path that do not exists

Posted on 2007-11-25
6
615 Views
Last Modified: 2013-12-03
Hi,

someone have a short sample how I can create a path of a given string?

Like Example: I get a CString with a path like: "C:\mypat\myfolder\mySubfolder" It is now posisble that the complete path is not existing or the last folder is not existing. What I need is a function or sample how to create the complet path or the last two folders or the last folder.
I use MFC so MFC is recommended.

Ingo
0
Comment
Question by:Ocrana
[X]
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
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20345741
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20346022
This is a fully POSIX compliant solution, it should work on both Windows and Linux: -
#ifdef WIN32
#include "direct.h"
#include "io.h"
#define DEFAULT_DELIM '\\'
#else
#include <sys/stat.h>
#include <unistd.h>
#define _access access
#define _mkdir(path) mkdir(path, 0777)
#define DEFAULT_DELIM '/'
#endif
 
#include "errno.h"
#include <string>
 
void MkDirs(std::string const & sFullPath, char const delim = DEFAULT_DELIM)
{
	std::string::size_type lpos = 0;
	std::string::size_type pos = 0;
 
	while(pos != sFullPath.size())
	{
		pos = sFullPath.find(delim, lpos);
		if(std::string::npos == pos) { pos = sFullPath.size(); }
 
		std::string const & sCurPath = sFullPath.substr(0, pos);
 
		if(!sCurPath.empty() && (-1 == _access(sCurPath.c_str(), 0x00)))
		{
			_mkdir(sCurPath.c_str());
		}
 
		lpos = pos + 1;
	}
}
 
int main()
{
#ifdef WIN32
	MkDirs("temp\\aaa\\bbb\\ccc\\");
#else
	MkDirs("temp/aaa/bbb/ccc/");
#endif
}

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 20346801
Either use 'SHCreateDirectory()' (http://msdn2.microsoft.com/en-us/library/ms647696.aspx) or if you can't use that for version reasone, try this lil' utility function that does that recursively:

DWORD       ChkCreateDir    (   char*   pszDir)
{
    char    acDir   [   MAX_PATH];
    char*   pc      =   NULL;
    DWORD   dwAtt   =   0;

    dwAtt   =   GetFileAttributes   (   pszDir);

    if  (   FILE_ATTRIBUTE_DIRECTORY    ==  dwAtt)
            return  (   dwAtt);

    strcpy  (   acDir,  pszDir);

    pc  =   strrchr (   acDir,  '\\');

    if  (   pc)
            *pc =   ( char) 0;
     else
            return  (   0xffffffff);

    dwAtt   =   ChkCreateDir    (   acDir);

    if  (   FILE_ATTRIBUTE_DIRECTORY    ==  dwAtt)
        {
            if  (   !CreateDirectory    (   pszDir, NULL))
                {
                    return GetLastError    ();
                }

            return  (   dwAtt);
        }

    return  (   0);
}

However, 'SHCreateDirectory()' definitely is easier to use, you'd just

#include <shlwapi.h>
#pragma comment(lib,"shlwapi.lib")

//...

SHCreateDirectory(NULL,"C:\\mypat\\myfolder\\mySubfolder");
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Ocrana
ID: 20386154
jkr: How to use this in Unicode?
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 20386173
Just

#include <shlwapi.h>
#pragma comment(lib,"shlwapi.lib")

//...

SHCreateDirectory(NULL,_T("C:\\mypat\\myfolder\\mySubfolder"));


or

DWORD       ChkCreateDir    (   wchar_t*   pszDir)
{
    wchar_t    acDir   [   MAX_PATH];
    wchar_t*   pc      =   NULL;
    DWORD   dwAtt   =   0;

    dwAtt   =   GetFileAttributes   (   pszDir);

    if  (   FILE_ATTRIBUTE_DIRECTORY    ==  dwAtt)
            return  (   dwAtt);

    wcscpy  (   acDir,  pszDir);

    pc  =   wcsrchr (   acDir,  L'\\');

    if  (   pc)
            *pc =   ( wchar_t) 0;
     else
            return  (   0xffffffff);

    dwAtt   =   ChkCreateDir    (   acDir);

    if  (   FILE_ATTRIBUTE_DIRECTORY    ==  dwAtt)
        {
            if  (   !CreateDirectory    (   pszDir, NULL))
                {
                    return GetLastError    ();
                }

            return  (   dwAtt);
        }

    return  (   0);
}
0
 

Author Comment

by:Ocrana
ID: 20386229
Hi jkr. Thanks, I opened another question, if you take a look:
http://www.experts-exchange.com/Programming/System/Windows__Programming/Q_22994614.html
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

628 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