I think I have to the code right cuz it all compiles perfectly except when its done running, and I go to my documents, I dont see my directory...

#include <iostream>
#include <windows.h>
#include <winuser.h>
#include <stdlib.h>
#include <string>
#include <stdio.h>
#include "stdafx.h"
#include <lm.h>
#include <assert.h>
#include <fstream>
#include <algorithm>
#include <shlobj.h>
#include <direct.h>


using namespace std;


int main()
{
     char user[100]="";
     DWORD size=100;
     GetUserName(user,&size);
     char path[255]="";
     sprintf(path,"C:\\Document And Settings\\%s\\My Documents\\",user);
     
     string str;    
     string base="C:\\Document And Settings\\user\\My Documents\\OMG";
     str=base;
     str.replace(25,4,user,0,100);
     
     LPSECURITY_ATTRIBUTES attr;
     attr = NULL;
     CreateDirectory(str.c_str(), attr);
     cout << "\nFolder Created!\n";
}

You could also do this for the same result, so that you can remove the whole attr part.

CreateDirectory(str.c_str(), NULL);

Your code seems fine. I don't know why you have added sprintf() but I surely did not need it to make a directory.

The only error you could have made is wrong paths, like Document and Settings instead of Document And Settings (lowercase/capital letters).

Edited 7 Years Ago by Kashaku: n/a

Why don't you try using the shlobj.h function SHGetSpecialFolderPath to get the path to the users My Documents folder? It would save all of the messing about your doing there!

Try replacing the code in your main function with this:

int main()
{
    // get the path to the current users my documents folder
    // storing it in an LPSTR
    LPSTR userDocsPath = new CHAR[MAX_PATH];
    SHGetSpecialFolderPath(0, userDocsPath, CSIDL_MYDOCUMENTS, 0);

    // now copy the path into a std::string
    std::string path = std::string(userDocsPath);

    // delete the LPSTR as we don't need it any more...
    delete userDocsPath;

    // Now you've got the path to your users 'My Documents' folder
    // stored in path, you can append any further directories onto it. 
    // e.g.
    path.append("/OMG"); 
    // NOTE: using forward slashes works in file paths, double backslashes are not necessary!

    // Not sure you need to bother creating an 
    // LPSECURITY_ATTRIBUTES object seeing as you're setting it to null!
    CreateDirectory(path.c_str(), 0);
    cout << "\nFolder Created!\n";
}

For more info take a look at the following pages:
SHGetSpecialFolderPath:
http://msdn.microsoft.com/en-us/library/bb762204(VS.85).aspx
CSIDL values:
http://msdn.microsoft.com/en-us/library/bb762494(VS.85).aspx

Hope that is of some help!
Cheers for now,
Jas.

Edited 7 Years Ago by JasonHippy: n/a

These are the new errors I get when using that code =(

In function `int main()':
22 `CSIDL_MYDOCUMENTS' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
22 C:\Documents and Settings\Owner\Desktop\Untitled1.cpp `SHGetSpecialFolderPath' undeclared (first use this function)

THX A BILLION well I searched my errors and solved it, seems like the CSIDL_MYDOCUMENTS is no longer a valid command, it changed to CSIDL_PERSONAL after windows 2000, also I had to put
#define _WIN32_WINNT 0x0500
#define _WIN32_IE 0x0500

before I input any of my includes...

This question has already been answered. Start a new discussion instead.