Raymun
asked on
Create Directory
Hello,
I'm given a string like "C:\SomeFolder\SomeFile.So meExtensio n";
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?
I'm given a string like "C:\SomeFolder\SomeFile.So
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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\SomeFolder 2", NULL );
You must make two calls:
CreateDirectory( "C:\SomeFolder1", NULL );
CreateDirectory( "C:\SomeFolder1\SomeFolder 2", NULL );
--Adrian
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\SomeFolder
You must make two calls:
CreateDirectory( "C:\SomeFolder1", NULL );
CreateDirectory( "C:\SomeFolder1\SomeFolder
--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.
That's why you can either use 'ShCreateDirectory()' or the above code to do that in one single call.
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. SomeExtens ion";
ChkCreateDir( dir );
File::Create( dir, 1024 );
}
Thanks!
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.
ChkCreateDir( dir );
File::Create( dir, 1024 );
}
Thanks!
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\\fi le.txt";
ChkCreateDir( dir );
File::Create( dir, 1024 );
}
#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\\fi
ChkCreateDir( dir );
File::Create( dir, 1024 );
}
#include <direct.h>
mkdir("c:\\SomeFolder");
For Windows platform, you can use CreateDirectory API function.
CreateDirectory("c:\\temp"