Creating Mulitple Levels of Directories

I was writing a custom installation script and needed to create many sub directories. I looked at Brad Gutilla's article and thought that there had to be a simpler method. Recursion steps in and saves the day. Not much to say about the code, as it is pretty simple and easy to follow.
void CreateAllDirectories(CString strDir)
{
 // remove ending / if exists
 if(strDir.Right(1)=="\\")
  strDir=strDir.Left(strDir.GetLength()-1); 

 // base case . . .if directory exists
 if(GetFileAttributes(strDir)!=-1) 
  return;

 // recursive call, one less directory
 int nFound = strDir.ReverseFind('\\');
 CreateAllDirectories(strDir.Left(nFound)); 

 // actual work
 CreateDirectory(strDir,NULL); 
}


Comments

  • I used MakeSureDirectoryPathExists successfully

    Posted by Legacy on 11/16/2001 12:00am

    Originally posted by: Adrian Vinca

    So, I used MakeSureDirectoryPathExists from Imagehlp.h (instead of Dbghelp.h - I don't know where may I find this file). Do not forget to add to your project also Imagehlp.lib.

    Reply
  • A much more optimized method

    Posted by Legacy on 08/13/2001 12:00am

    Originally posted by: Anonymous

    int
    
    CreateAllDirectories(
    char* pszDir
    )
    {
    char* pszLastSlash;
    char cTmp;

    if( _access( pszDir, 0 ) != -1 )
    {
    // it already exists
    return 0;
    }

    pszLastSlash = strrchr( pszDir, '\\' );
    if ( pszLastSlash )
    {
    cTmp = *pszLastSlash;
    *pszLastSlash = '\0';

    // try again with one less dir

    CreateAllDirectories( pszDir );

    *pszLastSlash = cTmp;
    }


    if ( _mkdir( pszDir ) == -1 )
    {
    return -1;
    }

    return 0;
    }

    Reply
  • why not to use the api...

    Posted by Legacy on 07/25/2001 12:00am

    Originally posted by: Kevin

    I looked and my default installation of VC and whatever SDKs I have installed do not have the Dbghelp.h file. So I am assuming that I would have to go thru the trouble of installing something else to get this. I am also assuming this means another DLL would have to go with my executable...

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

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds