Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create a Path that do not exists

Posted on 2007-11-25
6
Medium Priority
?
621 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Accepted Solution

by:
jkr earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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.
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
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.

730 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