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

How To Delete A Directory In C++ Builder On Windows

How can I delete a directory in C++ Builder? How can I use RmDir() in C++ ? What is RemoveDir method? How can I use the Delete method to delete a folder? How can I use the remove method of the filesystem in standard C++? Can I delete a folder by using systems commands in C++? Let’s answer these questions.

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.

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

How to delete a directory by using the RmDir Method in C++ Builder

RmDir Method (System::RmDir) is a System Library Method of C++ Builder that deletes an empty subdirectory.

Here is the Syntax of MkDir Method in C++ Builder:

RmDir removes the subdirectory with the path specified by S or P. If the path does not exist, is nonempty, or is the currently logged directory, an I/O error occurs. Note that in Delphi it handles run-time errors using exceptions. When using {$I-}, use IOResult to check for I/O errors.

Here is a simple example to MkDir() Method in C++ Builder,

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.


Is there an example of using the C++ MkDir method?

Here is a full example to MkDir Method.

Hwo can I delete a directory by using RemoveDir Method in C++ Builder?

The RemoveDir Method (System.SysUtils.RemoveDir) is a SysUtils Method that deletes an existing empty directory.

Call RemoveDir to remove the directory specified by the Dir parameter. The return value is True if a new directory was successfully deleted, False if an error occurred. The directory must be emptied before it can be successfully deleted.

Are there any special considerations for deleting symbolic links (symlinks)?

When working with symlinks, there are some special cases to consider because of how symlinks are implemented on different platforms. On Windows, RemoveDir can only delete a symbolic link from a directory, regardless if the directory link is broken or not.

Syntax:

Is there a C++ example of removing a directory?

Here is a simple example,

How to delete a directory or folder by using the delete Method in C++ Builder

The Delete Method (System::IOUtils::TDirectory::Delete) is a IOUtils Method listed in Disk And Directory Support Routines that deletes a directory at the given path.

Use Delete to delete a directory at the given path. The following table lists the parameters this method expects:

NameMeaning
PathPath of the directory being deleted.
RecursiveThe deletion is recursive. If this parameter is false, nonempty directories will not be deleted.

Syntax:

The second version of Delete does not expect a Recursive parameter; it is considered to be false. This means that the second version of Delete will fail on nonempty directories. Neither version of Delete reports whether the deletion operation succeeded. Note that Delete raises an exception if the given Path is invalid or contains invalid characters.

Is there an example of using the C++ RemoveDir method?

Here is a simple example,

How can I delete a directory or folder using the remove method of std::filesystem in C++?

C++ standard has a remove() 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,

Syntax:

A simple example,

How to delete a folder or 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,

Syntax for the system() command;

Here is an example that removes a folder by using system command,

Is there a full example of creating and deleting directories/folders 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,

Here are some more posts discussing the various folder and path manipulation methods: https://learncplusplus.org/?s=directories

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.

close

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

Get To Know The Powerful C++ Data Bindings In Windows Apps

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

How to Create a New Windows FMX Static Library In C++

C++C++11C++14C++17Introduction to C++Learn C++

How To Use C++ front() And back() Methods Of Vectors

C++Generic ProgrammingIntroduction to C++Language FeatureLearn C++

5 Things You Need To Know About Optimization In C++ Builder

en_USEnglish