Creating directories

I need help. I use win7, and I'm writing a program that will archive data. The last thing I need is a create directory function that not just creates the last folder, but also creates the subfolders which are the parent of that folder if they don't exist. EX: CreateDirectory(".\\Sub1\\Sub2\\archive\\"); won't work correctly if Sub1 and Sub2 don't exist. I would have to create them before I created Archive. I need a function that will create the entire directory in 1 stroke.

I heard of SHCreateDirectoryEx, but it only works with winXP or older. So, does someone know a way to do this? I'm trying to avoid using a Batch.
Surely we've been over this before.
http://www.cplusplus.com/forum/windows/76873/

Here's some code that ought to do it, but I just wrote it in the editor so haven't compiled or tested it. If you want to continue to get help, you really need to calm down.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
BOOL CreateFullDirectory(LPCTSTR dirName, LPSECURITY_ATTRIBUTES lpSA)
{
    TCHAR tmpName[MAXPATH];
    _tcscpy(tmpName, dirName);

    // Create parent directories
    for (LPTSTR p = _tcschr(tmpName, _T('\\'); p; p = _tcschr(p + 1, _T('\\')))
    {
        *p = 0;
        ::CreateDirectory(tmpName, lpSA);  // may or may not already exist
        *p = _T('\\');
    }

    return ::CreateDirectory(dirname, lpSA);
}
@IWishIKnew
SHCreateDirectoryEx is available on windows 7, and after looking through windows 8's version of shlobj.h it appears to be available on 8 as well. It isn't guaranteed to be available in future versions so kbw's solution is most likely a better option.
Last edited on
Topic archived. No new replies allowed.