C++Builder How To Check If A Directory On Windows Is Empty In C++?

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
How To Check If A Directory On Windows Is Empty In C++?
By Yilmaz Yoru September 28, 2021

How can I check if a Directory on Windows is Empty or Not? How can I use IsEmpty() Method on Directories? Let’s answer these questions.

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 Для просмотра ссылки Войди или Зарегистрируйся 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 or to get information about disks, directories and files. These methods are easy to remember, very friendly, and smart.

Let’s see how we use IsEmpty() Method in C++ Builder

How do I check a directory to see if it is empty using C++ Builder?​

The Для просмотра ссылки Войди или Зарегистрируйся Method (System::IOUtils::TDirectory::IsEmpty) is a SysUtils Method listed in Для просмотра ссылки Войди или Зарегистрируйся that checks whether a given directory is empty.

Here is the Syntax for the IsEmpty Method in C++ Builder:
C++:
static bool __fastcall IsEmpty(const System::UnicodeString Path);
We can call IsEmpty to check whether a given directory is empty. An empty directory is considered to have no files or other directories in it. IsEmpty returns true if the directory is empty; false otherwise. The following table lists the parameters expected by this method.

NameMeaning
PathPath of the directory being checked.
Note: If the Path parameter is an empty string, IsEmpty returns false.

Here is a simple example to IsEmpty() Method in C++ Builder,
C++:
bool empty = TDirectory::IsEmpty( L"D:\\MyFolder" );

Is there a full example of how to use the IsEmpty method in C++ Builder?​

Here is Full C++ Builder VCL example that creates a empty folder and checks if this directory is empty,
C++:
#include <vcl.h>
#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:\\MyFolder" );
 
 if( TDirectory::IsEmpty(L"D:\\MyFolder"))
 {
           ShowMessage("Folder is Empty");
 }
}
This method can be used before using the Remove Directory Methods like RmDir() or other methods, thus you can warn user that folder is not empty.