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

How can I create a directory in C++ Builder? How can I use the directory creation method including the creation of its subfolders? How can I use System Commands to Create a Directory? Can I use the DOS command to create a Folder? What is the MkDir method? How can I use the CreateDirectory method? Is there a example about std::filesystem::create_directory() method? Let’s see the answers to these questions.

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

Let’s see how we can create a directory by different methods.

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 creates a 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.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

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
C++Introduction to C++Language FeatureLearn C++

How To Generate A Random File Name on Windows

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

How To Include Or Exclude Trailing Backslashes On Windows

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

How To Check Characters of File Names and Paths on Windows

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

How to use SameFileName method on Windows