Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++CodeIDE

C++Builder Developer on StackOverflow was having trouble with SysUtils FindFirst / FindNext not finding all files with extension ~1~ and other strange file extensions.

Here was my answer 😀

Create a New | C++Builder VCL Windows application. On the form add TPanel, TButton, TEdit and TMemo components. Put the TButton and TEdit into the TPanel. Set TPanel align property to AlTop. Set the TMemo align property to alClient.

FindFirst’s declaration in SysUtils.hpp is:

extern DELPHI_PACKAGE int __fastcall FindFirst(const System::UnicodeString Path, int Attr, TSearchRec &F);

The key point is to included any or all of the file attributes (integer parameter Attr) for files that you want to find. The file attribute choices are defined in “C:\Program Files (x86)\Embarcadero\Studio\21.0\include\windows\rtl\windows\rtl\System.SysUtils.hpp”:

static const System::Int8 faInvalid = System::Int8(-1);  // Identifies an invalid file.
static const System::Int8 faReadOnly = System::Int8(0x1);  // Identifies read-only files or directories.
static const System::Int8 faHidden = System::Int8(0x2);  // Identifies hidden files or directories.
static const System::Int8 faSysFile = System::Int8(0x4);  // Identifies system files or directories.
static const System::Int8 faVolumeID _DEPRECATED_ATTRIBUTE0 = System::Int8(0x8);  // Deprecated
static const System::Int8 faDirectory = System::Int8(0x10);  // Identifies a directory.
static const System::Int8 faArchive = System::Int8(0x20);  // Identifies Windows archived files.
static const System::Byte faNormal = System::Byte(0x80);  // Identifies normal files.
static const System::Word faTemporary = System::Word(0x100);  // Identifies temporary files or directories.
static const System::Word faSymLink = System::Word(0x400);  // Specifies only symbolic link file types.
static const System::Word faCompressed = System::Word(0x800);  // Identifies a compressed file or directory.
static const System::Word faEncrypted = System::Word(0x4000);  // Identifies an encrypted file or directory.
static const int faVirtual = int(0x10000);  // Reserved for system use.
static const System::Word faAnyFile = System::Word(0x1ff);  // Specifies any file type.

The following code will display all items in a directory including the ., .., .~* files – for example in a C++Builder project’s history folder.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TSearchRec sr;
  Memo1->Lines->Clear();
  int iAttributes = 0;
  iAttributes |= faReadOnly;
  iAttributes |= faHidden;
  iAttributes |= faSysFile;
  iAttributes |= faVolumeID;
  iAttributes |= faDirectory;
  iAttributes |= faArchive;
  iAttributes |= faAnyFile;
  if (FindFirst(Edit1->Text, iAttributes, sr) == 0)
  {
	do
	{
	  if ((sr.Attr & iAttributes) == sr.Attr)
	  {
		Memo1->Lines->Add(
		  sr.Name
		  + " = "
		  + IntToStr(sr.Size)
		);
	  }
	} while (FindNext(sr) == 0);
	FindClose(sr);
  }
}

I put the string: C:\Users\david\Documents\Embarcadero\Studio\Projects\CBuilder\FindFirstVCLCpp\__history\*.* in the EditBox. After clicking the Button, the TMemo contained the following contents.

Screen Grab of running C++Builder FindFirst app

References

System.SysUtils.FindFirst

System.SysUtils.FindNext

To find more C++Builder StackOverflow questions and answers use the C++Builder SO Tag 😀

RAD Studio 13.1 Florence Now Available See What's New in RAD Studio 13.1 Delphi is 31 - Webinar Replay

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.

Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES