Solved

Create a Path that do not exists

Posted on 2007-11-25
6
606 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
Explain Unit of Work pattern 2 69
What the difference between blend and Visual Studio 3 174
What is sub-make ? 2 71
Need a complete list of ASP.NET page load events 3 59
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

860 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