Link to home
Start Free TrialLog in
Avatar of Raymun
Raymun

asked on

Create Directory

Hello,

I'm given a string like "C:\SomeFolder\SomeFile.SomeExtension";

I want to create SomeFile.SomeExtension under SomeFolder but SomeFolder may not exist, so SomeFolder may need to be created as well. What's the simplest way to do this?
Avatar of dkloeck
dkloeck
Flag of Spain image

You can use mkdir C/C++ function.

#include <direct.h>

mkdir("c:\\SomeFolder");

For Windows platform, you can use CreateDirectory API function.

CreateDirectory("c:\\temp", NULL);
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AdrianSRU
AdrianSRU

If you are using Windows, the CreateDirectory function will work:

CreateDirectory( "C:\SomeFolder", NULL );

The limitation of this function is that if you are trying to create several levels of directories you must create them one at a time.

If SomeFolder1 does not already exist this will fail:
CreateDirectory( "C:\SomeFolder1\SomeFolder2", NULL );

You must make two calls:
CreateDirectory( "C:\SomeFolder1", NULL );
CreateDirectory( "C:\SomeFolder1\SomeFolder2", NULL );


--Adrian
>>If SomeFolder1 does not already exist this will fail

That's why you can either use 'ShCreateDirectory()' or the above code to do that in one single call.

Avatar of Raymun

ASKER

Thanks dkloeck and Adrian but CreateDirectory's limitation of parent directory existence is not what I'm looking for.

jkr:
You code is almost exactly what I'm looking for. The only problem is it creates SomeFile.SomeExtension as a folder and not as a file. I modified a bit and it works perfectly now:

#using <mscorlib.dll>
#include <windows.h>

using namespace System::IO;

DWORD ChkCreateDir( char* pszDir )
{
    DWORD dwAtt = 0;
    dwAtt = GetFileAttributes( pszDir );

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

    char  acDir [MAX_PATH];
    strcpy( acDir,  pszDir );
   
    bool bCreateFolder = true;
   
    char* pc = NULL;
    pc  = strrchr( acDir, '\\' );
   
    if( pc ) {
        if( bCreateFolder ) bCreateFolder = false;

        *pc = (char) 0;  

    }
    else
        return( 0xffffffff );

    dwAtt = ChkCreateDir( acDir );

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

        return( dwAtt );
    }

    return( 0 );
}

int main()
{
    char* dir = "C:\\SomeFolder\\SomeFile.SomeExtension";
    ChkCreateDir( dir );
    File::Create( dir, 1024 );
}

Thanks!
Avatar of Raymun

ASKER

Sorry this is the working code:

#using <mscorlib.dll>
#include <windows.h>

using namespace System::IO;

DWORD ChkCreateDir( char* pszDir )
{
    DWORD dwAtt = 0;
    dwAtt = GetFileAttributes( pszDir );

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

    char  acDir [MAX_PATH];
    strcpy( acDir,  pszDir );
   
    bool bCreateFolder = true;
    static bool bIsFile = false;
   
    char* pc = NULL;
    pc  = strrchr( acDir, '\\' );
   
    if( pc ) {
        if( !bIsFile ) {
            bIsFile = true;
            bCreateFolder = false;
        }

        *pc = (char) 0;  

    }
    else
        return( 0xffffffff );

    dwAtt = ChkCreateDir( acDir );

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

        return( dwAtt );
    }

    return( 0 );
}

int main()
{
    char* dir = "D:\\Test\\Data\\Data2\\file.txt";
    ChkCreateDir( dir );
    File::Create( dir, 1024 );
}