How To Delete A Directory In C++ Builder On Windows (2024)

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 thispost,or we can use C++ standard library methods. In C++ Builder, we can use both of them and we can also useDisk And Directory Support Routinesto 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 */;

RmDirremoves 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-}, useIOResultto 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.MkDircreates only the last directory; it does not create parent directories, whereasForceDirectoriesdoes.


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.

CallRemoveDirto remove the directory specified by theDirparameter. The return value isTrueif a new directory was successfully deleted,Falseif 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 withsymlinks, there are some special cases to consider because of howsymlinksare implemented on different platforms. OnWindows,RemoveDircan only delete asymbolic linkfrom 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.

UseDeleteto 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 isfalse, nonempty directories will not be deleted.

Syntax:

1

2

3

TDirectory::RemoveDir( L"D:\\MyFolder");

The second version ofDeletedoes not expect aRecursiveparameter; it is considered to befalse. This means that the second version ofDeletewill fail on nonempty directories. Neither version ofDeletereports whether the deletion operation succeeded. Note that Deleteraises an exception if the givenPathis 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

How To Delete A Directory In C++ Builder On Windows (1)

C++ Builderis 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 fromhere. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download fromhere.

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

How To Delete A Directory In C++ Builder On Windows (2024)

References

Top Articles
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated:

Views: 6532

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.