Solved

Create a Path that do not exists

Posted on 2007-11-25
6
599 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
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

759 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

19 Experts available now in Live!

Get 1:1 Help Now