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:
- 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 system commands in C++?
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.
Table of Contents
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:
1 2 3 |
void __fastcall RmDir(const UnicodeString S)/* overload */; |
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,
1 2 3 |
RmDir( L"D:\\MyFolder1" ); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <vcl.h> #pragma hdrstop #include "Create_Directory_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { RmDir( L"D:\\MyFolder1" ); } |
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:
1 2 3 |
TDirectory::RemoveDir( L"D:\\MyFolder"); |
Is there a C++ example of removing a directory?
Here is a simple example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <vcl.h> #pragma hdrstop #include "Create_Directory_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { RemoveDir( L"D:\\MyFolder2"); } |
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:
Name | Meaning |
---|---|
Path | Path of the directory being deleted. |
Recursive | The deletion is recursive. If this parameter is false, nonempty directories will not be deleted. |
Syntax:
1 2 3 |
TDirectory::RemoveDir( L"D:\\MyFolder"); |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <vcl.h> #pragma hdrstop #include "Create_Directory_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { RemoveDir( L"D:\\MyFolder2"); } |
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:
1 2 3 |
bool remove( const std::filesystem::path& p ); |
A simple example,
1 2 3 4 5 6 7 8 9 |
#include <iostream> #include <filesystem> int main() { std::filesystem::remove("D:\\MyFolder3"); } |
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;
1 2 3 |
int system( const char *command ); |
Here is an example that removes a folder by using system command,
1 2 3 4 5 6 7 8 |
#include <iostream> int main() { std::system("rmdir D:\\MyFolder4"); } |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#include <vcl.h> #include <filesystem> #include <IOUtils.hpp> #pragma hdrstop #include "Create_Directory_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { MkDir( L"D:\\MyFolder1" ); RmDir( L"D:\\MyFolder1" ); TDirectory::CreateDirectory( L"D:\\MyFolder2" ); RemoveDir( L"D:\\MyFolder2"); std::filesystem::create_directory( L"D:\\MyFolder3" ); std::filesystem::remove( L"D:\\MyFolder3" ); std::system( "mkdir D:\\MyFolder4" ); std::system( "rmdir D:\\MyFolder4" ); ForceDirectories( L"D:\\MyFolder5\\MySubFolder\\MyOtherSubFolder"); RmDir( L"D:\\MyFolder5\\MySubFolder\\MyOtherSubFolder"); RmDir( L"D:\\MyFolder5\\MySubFolder" ); RmDir( L"D:\\MyFolder5" ); } |
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition