DirectoriesAndFilesEnumeraion (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This examples demonstrates the use of methods in TDirectory class to enumerate files or directories in a file system.

Code

void __fastcall TForm1::Button1Click(TObject *Sender)
{
	TStringDynArray list;
	TSearchOption searchOption;

	/* Select the search option */
	if (cbDoRecursive->Checked)
		searchOption = TSearchOption::soAllDirectories;
	else
		searchOption = TSearchOption::soTopDirectoryOnly;

	try
	{
		/* For all entries use GetFileSystemEntries method */
		if (cbIncludeDirectories->Checked && cbIncludeFiles->Checked)
		list = TDirectory::GetFileSystemEntries(edtPath->Text, searchOption, NULL);

		/* For directories use GetDirectories method */
		if (cbIncludeDirectories->Checked && !cbIncludeFiles->Checked)
		list = TDirectory::GetDirectories(edtPath->Text, edtFileMask->Text, searchOption);

		/* For files use GetFiles method */
		if (!cbIncludeDirectories->Checked && cbIncludeFiles->Checked)
		list = TDirectory::GetFiles(edtPath->Text, edtFileMask->Text, searchOption);
	}
	catch (...)
	{
		/* Catch the possible exceptions */
		MessageDlg("Incorrect path or search mask", mtError, TMsgDlgButtons() << mbOK, 0);
		return;
	}

	/* Populate the memo with the results */
	mmResults->Clear();

	for (int i = 0; i < list.Length; i++)
		mmResults->Lines->Add(list[i]);
}
//---------------------------------------------------------------------------

Uses