dcsimg
 

Make Sure Path Exists

Friday Mar 15th 2002 by Arlen Albert

Sure, you can use the API version of MakeSureDirectoryPathExists function to satisfy your needs, but here is an alternative way.

-->

Environment: not specified

An alternative way to the API MakeSureDirectoryPathExists function.

Introduction

Sure, you can use the API version of MakeSureDirectoryPathExists function to satisfy your needs, but there must be an alternative way. What do you need to do if you want to use the API MakeSureDirectoryPathExists function?

First, you have to include 'imagehlp.h' into your project file and point to the 'imagehlp.lib' file so you can link to it. The result is that you will never know how an API function is implemented. Additionally, the size of your released executable file that uses it will blow up.

I dare to present my two versions of a functions to perform the same task as MakeSureDirectoryPathExists does. The main purpose any of them is to make sure the required path exists. If it doesn't, the function creates the nessesary folders to fit the requested path. These functions are not recursive -- they are fast, small and universal.

How Does It Work?

Both versions works as the API MakeSureDirectoryPathExists function but a little bit occuratly. Note the second parameter of each function. It tells a function whether a filename is included to the path or isn't. It is necessary because you may create a file without an extension and thus there is no way to determine is there a folder name or a filename without an extension. The same, you may create a folder with an extension and here we go again, there is no way to determine is there a filename or a folder name with an extension. Only you can tell a function the way it has to work. When you provide a path a function does not care if there any trailing slash or back slash presents. The difference between the MFC version and the "C" version is that "C" version is a little bit faster, requires no additional memory usage and cares both the slash and the back slash symbols. You can pass CString object to the "C" version fuction and that's ok.

BOOL MakeSurePathExists( CString &Path, 
                         bool FilenameIncluded=true)
{
   int Pos=0;
   while((Pos=Path.Find('\\',Pos+1))!=-1) 
      CreateDirectory(Path.Left(Pos),NULL);
   if(!FilenameIncluded)
      CreateDirectory(Path,NULL);
   return ((!FilenameIncluded)?!_access(Path,0):
        !_access(Path.Left(Path.ReverseFind('\\')),0));
}

int make_sure_path_exists( const char *iPath, 
                           bool FilenameIncluded=true)
{
   char *Path=(char*)iPath, 
        *TmpPath=Path, 
        TmpSmb=0,
        *LastDPtr=NULL;
   while((TmpPath=strpbrk(TmpPath+1,"\\/")))
   {
      TmpSmb=Path[TmpPath-Path];
      Path[TmpPath-Path]=0;
      CreateDirectory(Path,NULL);
      Path[TmpPath-Path]=TmpSmb;
      LastDPtr=TmpPath;
   }

   int Res=1;
   if(!FilenameIncluded)
   {
      CreateDirectory(iPath,NULL);
      Res=!_access(iPath,0);
   }
   else
   {
      if(LastDPtr)
      {
         Path=(char*)iPath;
         TmpSmb=Path[LastDPtr-Path];
         Path[LastDPtr-Path]=0;
         Res=!_access(Path,0);
         Path[LastDPtr-Path]=TmpSmb;
      }
   }
   
   return Res;
}
Home
Mobile Site | Full Site
Copyright 2018 © QuinStreet Inc. All Rights Reserved