C++Builder The Easy Way To Create A Windows Directory In C++

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
The Easy Way To Create A Windows Directory In C++
September 3, 2021 By Yilmaz Yoru

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 Для просмотра ссылки Войди или Зарегистрируйся or we can use C++ standard library methods. In C++ Builder, we can use both of them and we can also use Для просмотра ссылки Войди или Зарегистрируйся 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​

Для просмотра ссылки Войди или Зарегистрируйся 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 Для просмотра ссылки Войди или Зарегистрируйся to check for an I/O error.

Here is the syntax of MkDir Method in C++ Builder​

C++:
void __fastcall MkDir(const UnicodeString S)/* overload */;

Here is a simple example of MkDir() Method in C++​

C++:
MkDir( 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 Для просмотра ссылки Войди или Зарегистрируйся does.

Here is a full example of how to use the MkDir method in C++​

C++:
#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)
{
 MkDir( L"D:\\MyFolder1" );
}

Creating a directory with the ForceDirectories method in C++ Builder​

The ForceDirectories method is explained in the following Для просмотра ссылки Войди или Зарегистрируйся

Creating Directory by using CreateDirectory Method in C++ Builder​

Для просмотра ссылки Войди или Зарегистрируйся 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,
C++:
static void __fastcall CreateDirectory(const System::UnicodeString Path);
The following table lists the parameters expected by this method.

NameMeaning
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,
C++:
TDirectory::CreateDirectory( L"D:\\MyFolder");

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,
C++:
#include <iostream>
#include <filesystem>
int main()
{
   std::filesystem::create_directory("D:\\MyFolder3");
}

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,
C++:
#include <iostream>
 
int main()
{
    std::system("mkdir D:\\MyFolder");
}

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++:
#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" );
}