🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
5 messages
5 total messages Started by tom Mon, 10 Dec 2007 03:24
Getting directory file names in builder 5
#853
Author: tom
Date: Mon, 10 Dec 2007 03:24
8 lines
1372 bytes
    Haven't been here in awhile and haven't used bcb5 in awhile either. What 
I would like to know is how can I get a listing of file names from a given 
directory in bcb5? Seems to me I did it before but for the life of me I 
can't remember how or in what program I did it.
    Any way does has this command:   dir /b MyDirName > temp.txt    This 
reads all the names in MyDirName and puts them into a text file. I 
remembered this from the dos days, and looking back in old books. Is ther a 
way in BCB5. thanks for any help. 
Re: Getting directory file names in builder 5
#854
Author: Dennis Jones
Date: Mon, 10 Dec 2007 05:43
15 lines
1567 bytes
"tom" <No@noemail.net> wrote in message 
news:475cb157@newsgroups.borland.com...
>    Haven't been here in awhile and haven't used bcb5 in awhile either. 
> What I would like to know is how can I get a listing of file names from a 
> given directory in bcb5? Seems to me I did it before but for the life of 
> me I can't remember how or in what program I did it.
>    Any way does has this command:   dir /b MyDirName > temp.txt    This 
> reads all the names in MyDirName and puts them into a text file. I 
> remembered this from the dos days, and looking back in old books. Is ther 
> a way in BCB5. thanks for any help.

See the Win32 API functions FindFirstFile/FindNextFile.

- Dennis 
Re: Getting directory file names in builder 5
#855
Author: Ed Mulroy [TeamB]
Date: Mon, 10 Dec 2007 17:01
80 lines
3334 bytes
If you are looking for a way to issue the command line directory request 
then look at the function 'system'.

If you are looking to do it with your own code, the Windows functions 
FindFirstFile, FindNextFile and FindClose are used for that.  Here is a 
console app that you might use as a starting point:

---------------------
#include <stdio.h>
#include <windows.h>

void PrintFileInfo(WIN32_FIND_DATA & file_info)
  {
  if (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    printf("<Dir> ");

  printf("%s\n", file_info.cFileName);
  } // end PrintFileInfo


int ListFilesInDir(const char *directory)
  {
  HANDLE find_handle;
  WIN32_FIND_DATA wfd;
  char find_pattern[MAX_PATH];
  int num_files_found = 0;

  // construct a search pattern string
  lstrcpy(find_pattern, directory);

  // end the name with "\\*.*"
  const char *str_to_append;

  if (*(find_pattern + strlen(find_pattern) - 1) == '\\')
    str_to_append = "*.*"; // it already has the backslash
  else
    str_to_append = "\\*.*";

  lstrcat(find_pattern, str_to_append);

  // search for the files
  find_handle = FindFirstFile(find_pattern, &wfd);

  // if a file was found
  if (find_handle != INVALID_HANDLE_VALUE)
    {
    do // loop to report what was found and find the next one
      {
      ++num_files_found;
      PrintFileInfo(wfd);
      } while (FindNextFile(find_handle, &wfd));

    FindClose(find_handle); // close the file search handle
    }

  return num_files_found;
  } // end ListFilesInDir


int main()
  {
  int num_found = ListFilesInDir("C:\\Windows");

  printf("%d files found\n", num_found);
  return 0;
  }
---------------------

> tom wrote in message
> news:475cb157@newsgroups.borland.com...
>
>    Haven't been here in awhile and haven't used bcb5 in awhile either. 
> What I would like to know is how can I get a listing of file names from a 
> given directory in bcb5? Seems to me I did it before but for the life of 
> me I can't remember how or in what program I did it.
>
>   Any way does has this command:   dir /b MyDirName > temp.txt    This 
> reads all the names in MyDirName and puts them into a text file. I 
> remembered this from the dos days, and looking back in old books. Is ther 
> a way in BCB5. thanks for any help.
Re: Getting directory file names in builder 5
#856
Author: Remy Lebeau (TeamB)
Date: Mon, 10 Dec 2007 17:45
12 lines
1333 bytes
"tom" <No@noemail.net> wrote in message 
news:475cb157@newsgroups.borland.com...

> What I would like to know is how can I get a listing of file names
> from a given directory in bcb5?

Look at the VCL's FindFirst(), FindNext(), and FindClose() functions.  Or 
the Win32 API's FindFirstFile(), FindNextFile(), and FindClose() functions.


Gambit 
Re: Getting directory file names in builder 5
#857
Author: Michael Corby
Date: Mon, 10 Dec 2007 18:22
114 lines
4695 bytes
"tom" <No@noemail.net> wrote in message 
news:475cb157@newsgroups.borland.com...
>    Haven't been here in awhile and haven't used bcb5 in awhile either. 
> What I would like to know is how can I get a listing of file names from a 
> given directory in bcb5? Seems to me I did it before but for the life of 
> me I can't remember how or in what program I did it.
>    Any way does has this command:   dir /b MyDirName > temp.txt    This 
> reads all the names in MyDirName and puts them into a text file. I 
> remembered this from the dos days, and looking back in old books. Is ther 
> a way in BCB5. thanks for any help.
>

Hi Tom

Code taken from one of my test programs :-

void __fastcall TwThirdForm::bDirListClick(TObject *Sender)
{
   String path, fileFilter, fullPathAndFilter, fileName, lines, header;
   String output;
   int index, size, txtLen;
   TSearchRec searchRec;

   index = DirectoryListBox1->ItemIndex;
   path = DirectoryListBox1->GetItemPath(index) + "\\";

   fileFilter = wThirdForm->eFilter->Text;
   txtLen = fileFilter.Length();
   if (txtLen == 0)   {   fileFilter = "*.*";   }   // All Files
   else               {   fileFilter = "*." + fileFilter;   }

   fullPathAndFilter = path + fileFilter;

   // Clear items from ComboBox List and Memo
   rProcsRunning->bStringBand->Items->Clear();  // Quick Report
   wThirdForm->Memo1->Lines->Clear();

   header = "List Sub-Directories and Files in Directory - " + path;
   wThirdForm->Memo1->Lines->Add(header);
   lines = "";
   wThirdForm->Memo1->Lines->Add(lines);

   // Get Directories
   if (FindFirst(fullPathAndFilter, faDirectory, searchRec) == 0);
   {
      if (searchRec.Attr == faDirectory && faDirectory == faDirectory)
      {
         fileName = String(searchRec.Name);
         if (fileName != "." && fileName != "..")
         {
            lines = "Directory - " + fileName;
            wThirdForm->Memo1->Lines->Add(lines);
            rProcsRunning->bStringBand->Items->Add(lines);
         }
      }
   }

   // Get Remainder of Directories
   while (FindNext(searchRec) == 0)
   {
      if (searchRec.Attr == faDirectory && faDirectory == faDirectory)
      {
         fileName = String(searchRec.Name);
         if (fileName != "." && fileName != "..")
         {
            lines = "Directory - " + fileName;
            wThirdForm->Memo1->Lines->Add(lines);
            rProcsRunning->bStringBand->Items->Add(lines);
         }
      }
   }
   // Close - always required after FindFirst/FindNext
   FindClose(searchRec);

   // Get Files
   FileListBox1->Directory = fullPathAndFilter;
   size = FileListBox1->Items->Capacity; // num files in directory

   for(int i=0; i<size; i++)
   {
      FileListBox1->ItemIndex = i;        // set index in filelist to i
      fileName = FileListBox1->FileName;  // absolute path of file
      // Extract File Name only
      output = ExtractFileName(fileName);
      wThirdForm->Memo1->Lines->Add(output);
      rProcsRunning->bStringBand->Items->Add(output);
   }

   // Report Header Details
   rProcsRunning->ReportDescription->Caption = header;

   // Report Date
   rProcsRunning->DateOfReport->Caption = DateOfReport();  // This is a 
function

   // Preview is not Modal - so disable calling form
   wThirdForm->Enabled = false;

   rProcsRunning->Report->PreviewWidth = 720;
   rProcsRunning->Report->PrevShowSearch = false;
   rProcsRunning->Report->PrevShowThumbs = false;
   rProcsRunning->Report->PrevFormStyle = fsNormal;

   rProcsRunning->Report->Preview();

   // Enable calling form
   wThirdForm->Enabled = true;
}
//---------------------------------------------------------------------------

I hope you can pick up what you need from this.

Michael Corby
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads