Originally posted by: Rolando E. Cruz-Marshall
This one function replaces the code mentioned in this article.
BOOL MakeSureDirectoryPathExists(LPSTR lpPath)
#include "imagehlp.h"
Place this in your .cpp file or in stdafx.h if you will use this function elsewhere in your program.
You will also need to include "imagehlp.lib" within the Link tab of the Project Settings dialog box.
Hope this helps.
Rolando
ReplyOriginally posted by: Aaron Clausen
Can anyone please tell me which files I need to inclue to make use of this function ?
Thanks
ReplyOriginally posted by: Remon
BOOL CreateDirectoryFromRoot( LPCTSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes = NULL )
if ( _tcsncmp( _T("\\\\?\\"), lpPathName, 4 ) != 0 )
CFileSpec::AppendBackSlash( strPath );
szBuffer = strPath.GetBuffer(strPath.GetLength());
//
. The '\\?\' string is prepended to the path so that an UNICODE version of this function can transcend the CreateDirectory limit (See MSDN).
Here is another simple way to create all directories of the given path...
{
CString strPath;
LPTSTR szBuffer;
int i;
strPath = _T("\\\\?\\") + CFileSpec::GetFullPath( lpPathName );
else
strPath = lpPathName;
strPath += _T("NUL");
// Try to create the subdirectories (if any) named in the path.
//
i = 4;
while ( szBuffer[i] != _T('\0') )
{
if ( szBuffer[i] == _T('\\') )
{
szBuffer[i] = _T('\0');
CreateDirectory(szBuffer, lpSecurityAttributes);
szBuffer[i] = _T('\\');
}
i++;
}
CFileAttributes fa;
return( fa.Exist( lpPathName ) && fa.IsDirectory() );
}
. The CFileSpec::GetFullPath() function returns the fully qualified pathname of the given string.
. The CFileSpec::AppendBackSlash function appends a directory separator to the given string if not already ended with one.
. The CFileAttributes class encapsulates the GetFileAttributes() call.
Originally posted by: Erich Hermann
Have a look at the online-help.
BOOL bRet = MakeSureDirectoryPathExists( "C:\\mydir\\mysub\\mysubsub\\" );
creates the whole dir-hierarchy.
Enjoy,
You've done too much work: There is a function called MakeSureDirectoryPathExists() in imagehlp.dll which was
introduced with Win95 (redistributable file for 95) and is available on WinNT.
Erich
Originally posted by: Meni Hillel
The following is much more simple :
// Check if directory exists
BOOL DirectoryExists(const CString Name)
{
DWORD Code = GetFileAttributes(Name);
return (Code != -1) && (Code & FILE_ATTRIBUTE_DIRECTORY );
}
//--------------------------------------------
// Create full path weather exists or not
BOOL ForceDirectories(CString Dir)
{
if (Dir.IsEmpty())
return TRUE;
CString Path = ExtractFilePath(Dir);
if (Dir.GetLength() < 3 || DirectoryExists(Dir) || Path == Dir)
return TRUE; // avoid 'xyz:\' problem.
ForceDirectories(Path);
return CreateDirectory(Dir, NULL);
}
//--------------------------------------------
// Extract file Path name - basically go up one level (search for the first '\' or ':')
CString ExtractFilePath(CString FileName)
{
CString Path;
int i=-1;
i = FileName.ReverseFind( '\\' );
if (i==-1)
i = FileName.ReverseFind( ':' );
if (i>=0)
Path = FileName.Left(i);
else
Path = FileName;
return Path;
}
//--------------------------------------------
Reply
Originally posted by: Markus Loibl
// path exists
bErg = SplitFullPath(szFullFilename, szRetPath, szFilename, szExtension);
return bErg;
memset(szPath, 0, _MAX_PATH);
if (!GetFullPathName(szFullFilename, _MAX_PATH, szPath, &szFile) )
return TRUE;
// you can replace GetPathAndFilename() by some Win32-Function
/ /but it is a helper that one can use in more situations
BOOL MakeDirInclSubDirs(const char *szFullDirname)
{
if( szFullDirname==NULL )
return FALSE;
if( *szFullDirname=='\0' )
return FALSE;
if( _access(szFullDirname, 00) == -1 )
{
// doesnt exist
char szTemp[2*_MAX_PATH];
ZeroMemory(szTemp, 2*_MAX_PATH);
// one up
if( !GetPathAndFilename(szFullDirname, szTemp, NULL) )
return FALSE;
// nothing happened
if( strcmp(szFullDirname, szTemp) == 0 )
return FALSE;
// try again
if( MakeDirInclSubDirs(szTemp) )
{
// now make subdir
return ( _mkdir(szFullDirname) == 0 );
}
else
return FALSE;
}
return TRUE;
}
BOOL GetPathAndFilename(const char *szFullFilename, char *szRetPath, char *szRetFile)
{
char szExtension[_MAX_PATH],
szFilename[2*_MAX_PATH];
BOOL bErg;
if( bErg && szRetFile )
sprintf(szRetFile, "%s.%s", szFilename, szExtension);
}
BOOL SplitFullPath(const char *szFullFilename, char *szRetPath, char *szRetFile, char *szRetExtension)
{
char szPath[_MAX_PATH],
*pPos;
LPTSTR szFile;
return FALSE;
// Pfad
pPos = strrchr(szPath, '\\');
if( pPos != NULL )
{
*pPos = '\0';
if( szRetPath != NULL )
strcpy(szRetPath, szPath);
pPos++; // Damit auf den Dateiname
}
else
pPos = (char *) szFullFilename; // Pfad ohne \\
// Extension
strcpy(szPath, pPos);
pPos = strrchr(szPath, '.');
if( szRetExtension )
if( pPos )
strcpy(szRetExtension, pPos+1);
else strcpy(szRetExtension, "");
// Name
if( pPos )
*pPos = '\0';
if( szRetFile )
strcpy(szRetFile, szPath);
}
Originally posted by: Rick
Rick
First of all, good work. I have seen questions about
this asked numerous times on the message board lately.
Now if we can only get those folks to look at the articles :)
A minor correction : CreateDirectory is not a wrapper for
_mkdir. It is the other way around : _mkdir is the wrapper
for CreateDirectory. _mkdir is in the C Run-Time Library
and CreateDirectory is the Win32 library function and is in
kernel32.lib. You can see this by looking at
\DEVSTUDIO\VC\CRT\MKDIR.C line 44 for VC v5.0 where
CreateDirectory is called by _mkdir. On a general basis,
things in the CRT lib are wrappers of things in the Win32
library, if they wrap anything. As I said before, this is
a minor correction to a very useful piece of work.
Reply