Site icon Learn C++

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

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.

In this post, you’ll get answers to these questions:

By learning how to delete a directory in C++ Builder on windows, and how to compile c++ in windows. It will help you to easily build C++ applications.

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:

[crayon-6763967c896ca194986919/]

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,

[crayon-6763967c896d1608016723/]

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.

[crayon-6763967c896d3775745965/]

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:

[crayon-6763967c896d4217743543/]

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

Here is a simple example,

[crayon-6763967c896d6449864427/]

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:

[crayon-6763967c896d7865463324/]

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,

[crayon-6763967c896d9910667227/]

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:

[crayon-6763967c896da728907567/]

A simple example,

[crayon-6763967c896db080269968/]

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;

[crayon-6763967c896dc884142772/]

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

[crayon-6763967c896dd542719290/]

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,

[crayon-6763967c896de944556794/]

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.

Exit mobile version