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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
CC++C++17Learn C++

How To Compile And Run A C Program

CC++Introduction to C++Learn C++

What Kind Of Programming Language Is C?

CC++Introduction to C++Learn C++

What Does C Mean In Programming?

CC++Introduction to C++Learn C++

How To Compile A C Program?