C++Introduction to C++Language FeatureLearn C++

The Easy Way To Create A Windows Directory In C++

The Easy Way To Create A Windows Directory In C++

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.

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

Here is a simple example of MkDir() Method in C++

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++

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,

The following table lists the parameters expected by this method.

PathPath 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,

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,

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,

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,

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.


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is An Eligible Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What Is A Trivial Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What Is Uppercase T in C++ Syntax?

C++C++11C++14C++17Learn C++Syntax

What Is An Implicitly-defined Copy Assignment Operator In C++?