How to make a program create a new folder

Pages: 123
Hi ,


I would like to know how can i make my program create a new folder in the directory it is , so when i make the int cFolder:
1
2
3
4
5
int cFolder = 0;

cout << "1.Add Folder 2.Remove Folder" << endl:
cout << "What would you like to do: " << endl;
cin >> cFolder;


Then the user gets this :
1
2
3
4
5
6
7
8
if (cFolder == 1)
{
   Add Folder
}
else
{
   Remove Folder
}


So my question is how to make the program add/remove folder named "New Folder" or "Test Folder" or something like that ?

This is a platform specific operation. Are you writing on Windows, *nix or another Operating System?

It almost goes without saying but you still need to have delete permissions on the target directory for this to work.
Im writing on windows and ill sort out permisions later but can you give me a specific command ?
Thanks :D

But can you give me a sample command i cant get it to work
Last edited on
Try this:
if(!CreateDirectory(TEXT("some_path"), 0)) std::cout<<"Failed"<<std::endl;
Last edited on
closed account (3hM2Nwbp)
Before any of the aforementioned code will work, you'll have to #include<Windows.h> in order to find the winapi functions if you already haven't.
Just in case you wanted non platform specific stuff, you could check out Boost Filesystem. However, it does involve building the library yourself, so it might not be the best option if you are a beginner.
http://www.boost.org/doc/libs/1_46_1/libs/filesystem/v3/doc/reference.html

If you do get everything working, you just include #include <boost/filesystem.hpp> .
And then the code is boost::filesystem::create_directory(directory_name).
Last edited on
Still cant get the windows one to work , ill try the Boost Filesystem thing :D
You'll have to download and build boost - let us know if you need help with that :) Once it's installed it's easy, but it can be tricky to get that far if you're new...
OK so i used boost pro installer and now im following this guide http://wiki.codeblocks.org/index.php?title=BoostWindowsQuickRef

It says
In the "include" field, browse for the "include\boost-1_42" subfolder of your Boost installation -- it should be the path in the "base" field with "\include\boost-1_42" tacked on.


But i cant find the include folder ? help ?
That's a misguiding tutorial, it would seem. There is no include/ directory. Just add the base boost directory as an include path. The header files are inside a directory in the main boost directory called boost/ (you can look!).

So if you add the base boost directory as a compiler search path, you can incude boost headers like this:
#include <boost/header_file_name.hpp>
Still cant get it to work maybe you could suggest a different workspace then codeblocks i could try to use it with ?
Last edited on
Visual Studio? Nonetheless, this problem won't be helped by switching IDEs. Can you tell me the absolute path where you put boost, and the absolute path of the boost folder containing "bjam.exe", "boost.css", "jamroot" and other files.
Boost install dir :
C:\Program Files (x86)\boost\boost_1_46_1

Boost Folder with bjam.exe
C:\Program Files (x86)\boost\boost_1_46_1\bin
Is there a subdirectory of C:\Program Files (x86)\boost\boost_1_46_1 called "boost", which contains more folders, and a load of *.hpp (header) files?
Yes there is

C:\Program Files (x86)\boost\boost_1_46_1\boost
Still cant get the windows one to work , ill try the Boost Filesystem thing
If you can't get the Windows one to work, you haven't a hope of getting the Boost one to work.
Yeah i will , with a little help that is
Last edited on
@OP
Okay, so that is the actual include folder. Presumably you can now continue with the Wiki guide.

However, kbw may have a point. We can see how this goes, but when it comes to building Boost, it might be tricky as I said. But there's no harm in trying :) Just don't get put off if it seems daunting at first.
Last edited on
Pages: 123