FileListBox (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example uses a file list box and a regular list box on a form. The following routine scans through the files listed in the file list box and lists the sizes of any selected files to the regular list box. To exercise the error condition, create a file in the Debug directory, start this application, and then delete the file. Now try to list the size of the deleted file. Choosing Continue continues listing file sizes in a multiple selection that includes the deleted file. Choosing Break stops the operation. Set the MultiSelect and ExtendedSelect properties on the FileListBox.

Code

#include <stdio.h>     // For FILE, fopen, fstat, fileno, fread and fclose
#include <sys\stat.h>  // For fstat and the stat struct

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  for (int i = 0; i < FileListBox1->Items->Count; i++)
  {
	if (FileListBox1->Selected[i])
	{
	  if (!FileExists(FileListBox1->Items->Strings[i]))
	  {
		MessageBeep(0);
		if (Application->MessageBox(
		  String("File " + FileListBox1->Items->Strings[i] + "not found").c_str(),
		  NULL,
		  MB_OKCANCEL) == IDCANCEL)
		  break;
		else
		  continue;
	  }
	  FILE *F = _wfopen(FileListBox1->Items->Strings[i].c_str(), L"r");

	  struct stat statbuf;
	  fstat(fileno(F), &statbuf);
	  ListBox1->Items->Add(
		  FileListBox1->Items->Strings[i] + ": " + IntToStr(int(statbuf.st_size)));
	  fclose(F);
	}
  }
}

Uses

Personal tools
Assistance