C++ has a lot of great libraries to operate on every case, on every item. We can create or delete directories by using System Commands that we explained before in this post, or we can use C++ standard library methods. In C++ Builder, we can use both of them and we can also use Disk And Directory Support Routines to create or remove directories. These methods are easy to remember, very friendly, and smart.
In this post, you’ll learn how to make a directory in C++ software, how to use the directory creation method, including the creation of subfolders, how to use System Commands to Make a Directory, how to use the DOS command to Make a Folder, and what the MkDir method is. Examples of how to use the CreateDirectory method and the std::filesystem::create directory() method.
Table of Contents
Creating a directory (or folder) with the MkDir method in C++ Builder
MkDir Method (System::MkDir) is a System Library Method of C++ Builder that can be used to create new subdirectories.
It supports UnicodeStrings. MkDir creates a new subdirectory with the path specified by string S or P.
Note: In Delphi, {$I+} handles run-time errors using exceptions. When using {$I-}, use IOResult to check for an I/O error.
Here is the syntax of MkDir Method in C++ Builder
1 2 3 |
void __fastcall MkDir(const UnicodeString S)/* overload */; |
Here is a simple example of MkDir() Method in C++
1 2 3 |
MkDir( L"D:\\MyFolder1" ); |
The last item in the path cannot be an existing file name. MkDir creates only the last directory; it does not create parent directories, whereas ForceDirectories does.
Here is a full example of how to use the MkDir method in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <vcl.h> #pragma hdrstop #include "Create_Directory_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { MkDir( L"D:\\MyFolder1" ); } |
Creating a directory with the ForceDirectories method in C++ Builder
The ForceDirectories method is explained in the following post.
Creating Directory by using CreateDirectory Method in C++ Builder
CreateDirectory Method (System.IOUtils.TDirectory.CreateDirectory) is a IOUtils Method that creates a new directory at the given path. We can use CreateDirectory from the <filesystem> library to create a new directory at the given path. If the directories given in the path do not yet exist, CreateDirectory attempts to create them.
Here is the Syntax for the CreateDirectory() Method,
1 2 3 |
static void __fastcall CreateDirectory(const System::UnicodeString Path); |
The following table lists the parameters expected by this method.
Name | Meaning |
---|---|
Path | Path of the directory being created. |
Note: CreateDirectory raises an exception if the given Path is invalid or contains invalid characters.
Here is a simple example to CreateDirectory() Method,
1 2 3 |
TDirectory::CreateDirectory( L"D:\\MyFolder"); |
Creating Directory by using create_directory std::filesystem method in C++
C++ standard has a create_directory() method that comes with C++ 17 standards. We can also use this method in C++ applications and in C++ Builder applications as in example below,
1 2 3 4 5 6 7 8 |
#include <iostream> #include <filesystem> int main() { std::filesystem::create_directory("D:\\MyFolder3"); } |
Creating Directory by using with System Commands in C++
On Windows and some other operating systems you can use std::system() command to use System Commands like cd, mkdir rmdir commands etc. For example we can create a folder by using mkdir command and we can copy folder to another folder by using xcopy command and we can remove a folder by using rmdir command. See example below,
1 2 3 4 5 6 7 8 |
#include <iostream> int main() { std::system("mkdir D:\\MyFolder"); } |
Full Examples to Create and Delete Directories with Different Methods in C++ Builder
Here we used all methods above in a single C++ Builder example which shows how it is flexible to use different methods,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <vcl.h> #include <filesystem> #include <IOUtils.hpp> #pragma hdrstop #include "Create_Directory_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { MkDir( L"D:\\MyFolder1" ); RmDir( L"D:\\MyFolder1" ); TDirectory::CreateDirectory( L"D:\\MyFolder2" ); RemoveDir( L"D:\\MyFolder2"); std::filesystem::create_directory( L"D:\\MyFolder3" ); std::filesystem::remove( L"D:\\MyFolder3" ); std::system( "mkdir D:\\MyFolder4" ); std::system( "rmdir D:\\MyFolder4" ); ForceDirectories( L"D:\\MyFolder5\\MySubFolder\\MyOtherSubFolder"); RmDir( L"D:\\MyFolder5\\MySubFolder\\MyOtherSubFolder"); RmDir( L"D:\\MyFolder5\\MySubFolder" ); RmDir( L"D:\\MyFolder5" ); } |
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition