[Back To Directory]

Search All Files & Folders:

1. Open Borland C++ Builder.
2. Place a DiveCombo box on the form.
3. Place a Button on the form.
4. Place another Button on the form.
5. Place a Label on the form.
6. Place another Label on the form below Label1.
7. Place a ListView box on the form (found under the Win32 tab) set the width to around 500 and the height around 300.
8. Select ListView1 and goto the Object Inspector and change the ViewStyle to vsReport.
9. Right click ListView1 and click Colomns Editor and add a colomn naming it anything with a width of 500.
10.Double click Button1.
11. Type the following red code in the Button1Click function:

//This is what the Button1Clickton function should look like:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   char szDrive[256];
   AnsiString asDrive;

   gnCount = 0;
   ListView1->Clear();
   gbStopSearch = FALSE;

   asDrive = DriveComboBox1->Drive;
   strcpy(szDrive, asDrive.c_str());
   strcat(szDrive, ":\\");
   SetCurrentDirectory(szDrive);
   strcat(szDrive,"*.*");

   FindFiles(szDrive);
}
//---------------------------------------------------------------------------


12. Don't compile yet, add the following function prototypes and global variables near the top of Unit1.cpp:

BOOL gbStopSearch = FALSE;
int gnCount = 0;
void FindDirs(char * STR);
void FindFiles(char * STR);

13. Go to the end of Unit1.cpp and add the following 2 functions:

//////////////////////
//FindFiles Function//
//////////////////////
void FindFiles(char * STR)
{
   WIN32_FIND_DATA w32;
   TListItem *LST;

   char szDrive[MAX_PATH], szDir[MAX_PATH], szFile[MAX_PATH],
   szExt[MAX_PATH], szFullPath[MAX_PATH];

   HANDLE hFile = FindFirstFile(STR, &w32);
   BOOL bFinding = TRUE;

   while(bFinding)
   {
      Application->ProcessMessages();
      if(gbStopSearch)
      break;

      if(!(w32.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
      {
         GetFullPathName(w32.cFileName, MAX_PATH, szFullPath, NULL);
         Form1->Label1->Caption = w32.cFileName;
         LST = Form1->ListView1->Items->Add();
         LST->Caption = szFullPath;
         Form1->Label2->Caption = gnCount++;
         Form1->Update();
      }
      bFinding = FindNextFile(hFile, &w32);
   }
   FindDirs(STR);
}


//////////////////////
//FindDirs Function//
//////////////////////
void FindDirs(char * STR)
{
   WIN32_FIND_DATA w32;
   HANDLE hDir = FindFirstFile(STR, &w32);
   BOOL bFinding = TRUE;

   char tmpstr[MAX_PATH], shrtpath[MAX_PATH];

   while(bFinding)
   {
      Application->ProcessMessages();
      if(gbStopSearch)
      break;

      if((w32.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp(w32.cFileName, ".") && strcmp(w32.cFileName, ".."))
      {
         GetFullPathName(w32.cFileName, MAX_PATH, tmpstr, NULL);
         GetShortPathName(tmpstr, shrtpath, MAX_PATH);
         SetCurrentDirectory(shrtpath);
         strcat(shrtpath, "\\*");
         FindFiles(shrtpath);
      }
      bFinding = FindNextFile(hDir, &w32);
   }
   SetCurrentDirectory("..");
}

14. Double click Button2 and type the following red code in the Button2Click function:

void __fastcall TForm1::Button2Click(TObject *Sender)
{
   gbStopSearch = TRUE;
}
//---------------------------------------------------------------------------


15. Run the program, select a hard drive (not a floppy or a CDROM, it will fail) you can add error trapping to stop that if you like. Click Button1 and the program will scan the selected drive and display the full path of each file found in the ListView box. Label1 will display each file name found and Lable2 will diplay the overall file count. Click Button2 to stop the search. You can click Button1 again and the scan will start again from the beginning



[Back To Directory]