Solved

Create a Path that do not exists

Posted on 2007-11-25
6
605 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ClickOnce Install - Shortcut Question 3 97
Detect file exist or not 3 160
Using MS Code on my Mac 6 68
passing data from one form to another form in c++ 27 80
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

831 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