Make Sure Path Exists | CodeGuru

Make Sure Path Exists

–> 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’ […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 15, 2002
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

–>

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;
}
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.