Make Sure Path Exists


Application Security Testing: An Integral Part of DevOps


Environment: not specified

An alternative way to the API MakeSureDirectoryPathExists function.


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;
   return ((!FilenameIncluded)?!_access(Path,0):

int make_sure_path_exists( const char *iPath, 
                           bool FilenameIncluded=true)
   char *Path=(char*)iPath, 

   int Res=1;
   return Res;


  • Isn't BOB a weenie?!?

    Posted by Legacy on 04/01/2003 12:00am

    Originally posted by: joe

    He doesn't do anything useful or have any original thoughts, so he just goes around knocking other's efforts.
    You suck BOB!

  • Thanks

    Posted by Legacy on 03/31/2003 12:00am

    Originally posted by: Nosser

    Just what I was looking for...

    ps. I noticed lots of comments on this codesnip misstaking it as a check for filepath existence... Must have been frustrating :P

  • Cool down - btw nice code

    Posted by Legacy on 10/31/2002 12:00am

    Originally posted by: Teddy

    You made a nice code, Boy. Change your tune (R U some Russian?) nobody is against you. Those are visiting this site are not idiots beleive me, are they? So do not put capitals and comments on people who made some comment on your code and not on you. It is not a personal attack.
    Good job however with some small missing items.

  • An API exists for this job

    Posted by Legacy on 10/08/2002 12:00am

    Originally posted by: BOB

    The API

    BOOL PathFileExists(
    LPCTSTR pszPath

    Already does this. It is from shlwapi.lib

  • FindFirstFile network access problem

    Posted by Legacy on 09/10/2002 12:00am

    Originally posted by: Dennis


    I wrote a C++ console code to parse a network
    directory as follow :

    FILETIME filet;
    HANDLE FndHnd;
    WIN32_FIND_DATA FndInf;
    BOOL FndFlg;
    long maxtime = 0;
    char* fDir = "\\\computerA\\Dir1\\Dir2\\";

    FndHnd = FindFirstFile(fDir, FndInf);
    FndFlg = FndHnd != InvHndVal;
    while (FndFlg) // While a file has be found.
    filet.dwHighDateTime =
    if(maxtime < filet.dwHighDateTime)
    maxtime = filet.dwHighDateTime;
    memset(fname, '\0', 72);
    strcpy(fname, FndInf.cFileName);
    FndFlg = FindNextFile(FndHnd,&FndInf);

    and I encountered with problem at this line:

    FndHnd = FindFirstFile(fDir, FndInf);

    it seems like it could not recognize the network
    directory eventhough I already shared those

    Did I miss something? Please enlighten me

    Thanks in advance


  • I respect your codes anyway...

    Posted by Legacy on 05/08/2002 12:00am

    Originally posted by: Joery Wen

    yeah, good job!

    really take us lots of time and something else, for examples, eyesight, to explore the way we need in MSDN or somewhere else, for example, on the Internet.

    Thus I sincerely respect your work. I just have written a little piece of codes doing nearly the same thing before I remember to find more here...//faint. It took me some time. and really take me some eyesight,hehe.

    really good job!

    keep doing!

  • An easier way

    Posted by Legacy on 03/16/2002 12:00am

    Originally posted by: rod

    BOOL Exist(const CString& sFilespec)
    WIN32_FIND_DATA fd;

    HANDLE hFind = FindFirstFile(sFilespec, &fd);

    if (hFind == INVALID_HANDLE_VALUE)
    return FALSE;

    return TRUE;


    if ( Exist("c:\\windows\\*.*"))


  • I Forgot To Say...

    Posted by Legacy on 03/15/2002 12:00am

    Originally posted by: Arlen Albert Keshabian

    To use _access function you must include <io.h> header file.
    In some cases in order to use strpbrk function you must include <string.h> header file.

    Best wishes and regards,

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.