Click here to Skip to main content
15,891,607 members
Articles / Desktop Programming / MFC

Easy Installer

Rate me:
Please Sign up or sign in to vote.
4.81/5 (57 votes)
14 Sep 2006CPOL11 min read 277.9K   3.7K   155  
The Easy Installer program
// This code is from an article by Arlen Albert at:
// http://www.codeguru.com/Cpp/W-P/files/folderdirectorymaintenance/article.php/c4469/

#include "stdafx.h"
#include <io.h>

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

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Israel Israel
lichen farmer, semi-lingual, never saw a Sylvester Stallone movie

Comments and Discussions