C++Builder How To Use ForceDirectories Method To Create Folders In C++

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
How To Use ForceDirectories Method To Create Folders In C++
By Yilmaz Yoru September 1, 2021

ForceDirectories Method (System:SysUtils:ForceDirectories) is a SysUtils Method listed in Для просмотра ссылки Войди или Зарегистрируйся that creates a new directory, including the creation of parent directories as needed. ForceDirectories creates a new directory as specified in Dir, which must be a fully-qualified path name. If the directories given in the path do not yet exist, ForceDirectories attempts to create them.

Here is the Syntax of ForceDirectories Method in C++ Builder​

C++:
bool __fastcall ForceDirectories(System::UnicodeString Dir);
ForceDirectories returns True if it successfully creates all necessary directories, False if it could not create a needed directory.

Note that, do not call ForceDirectories with an empty string. Doing so causes ForceDirectories to raise an exception. The Vcl.FileCtrl unit (Windows only) contains a ForceDirectories function that is deprecated. System.SysUtils.ForceDirectories is preferred, and can be used in multi-device applications.

Here is a simple example of using the ForceDirectories() Method in C++​

C++:
ForceDirectories( L"D:\\MyFolder\\MySubFolder\\MyOtherSubFolder");

A full examples of how to create folders with the C++ ForceDirectories Method​

Here is the full simple example to ForceDirectories() method,
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)
{
 ForceDirectories( L"D:\\MyFolder\\MySubFolder\\MyOtherSubFolder");
}
This example above will create “MyFolder”, “MySubFolder” and “MyOtherSubFolder” directories together.

Here’s another example showing how to create folders or directories in C++​

Here we used different creating directory methods 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" );
 
 TDirectory::CreateDirectory( L"D:\\MyFolder3");
 
 std::filesystem::create_directory( L"D:\\MyFolder2");
 
 std::system( "mkdir D:\\MyFolder4");
 
 ForceDirectories( L"D:\\MyFolder5\\MySubFolder\\MyOtherSubFolder");
}