DirectoryOperations (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This examples demonstrates the use of methods in TDirectory class to create, delete, copy a directory, or to check if the specified directory is empty.

Code

void __fastcall TForm1::btnIsEmptyClick(TObject *Sender)
{
	try
	{
		/* Check if the specified directory is empty */
		if (TDirectory::IsEmpty(edSourcePath->Text))
		  MessageDlg("The specified directory is empty", mtInformation, TMsgDlgButtons() << mbOK, NULL);
		else
		  MessageDlg("The specified directory is not empty", mtInformation, TMsgDlgButtons() << mbOK, NULL);
	}
	catch (...)
	{
		/* Catch the possible exceptions */
		MessageDlg("Incorrect path", mtError, TMsgDlgButtons() << mbOK, 0);
		return;
	}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnCopyClick(TObject *Sender)
{
	try
	{
		/* Copy directory from source path to destination path */
		TDirectory::Copy(edSourcePath->Text, edDestinationPath->Text);
	}
	catch (...)
	{
		/* Catch the possible exceptions */
		MessageDlg("Incorrect source path or destination path", mtError, TMsgDlgButtons() << mbOK, NULL);
		return;
	}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnCreateClick(TObject *Sender)
{
	try
	{
		/* Create directory to specified path */
		TDirectory::CreateDirectory(edSourcePath->Text);
	}
	catch (...)
	{
		/* Catch the possible exceptions */
		MessageDlg("Incorrect path", mtError, TMsgDlgButtons() << mbOK, NULL);
		return;
	}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnDeleteClick(TObject *Sender)
{
	Boolean isRecursive;
	/* Check if the deletion method is recursive */
	if (cbIsRecursive->Enabled)
		isRecursive = true;
	else
		isRecursive = false;

	try
	{
		/* Delete directory from specified path */
		TDirectory::Delete(edSourcePath->Text, isRecursive);
	}
	catch (...)
	{
		/* Catch the possible exceptions */
		MessageDlg("Incorrect path", mtError, TMsgDlgButtons() << mbOK, NULL);
		return;
	}
}
//---------------------------------------------------------------------------

Uses

Personal tools
Assistance