Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading subdirs findfirst..!

Status
Not open for further replies.

801119

Programmer
Apr 10, 2000
311
SE
Hey all..!!

I am building a program that will read a CD and then save it's contents to a file..

the problem is to search all subdirs! Is there any easy way to do this?

I am using findfirst(..)

!thanks..!!
Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
 
I'm not aware of any "easy" way to do this. I used this method to create a similar function. I did read a 4-part article in C++ Builder Developer's Journal in which the author (Mark G. Wiseman) created some classes to list files. His classes included include or exclude properties. Their web site is and the article was entitled "Finding Files." You may have to be a subscriber, I don't know.

If that is not to your liking, post some of your code here and we'll see what we can do.
James P. Cottingham
 
Ok....So there is no easy way to do this!!

I can only search one dir!! And when there is more then one folder...trouble!!

// This is the code I use to search current dir for additional folders!! One other problem is, that with the use of *. it will also list all files with no extension

//--------- note that I did not write this code my self most was found in the help files that came with BCB3 standard
#include <dir.h>
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int done;
struct ffblk ffblk;
chdir(&quot;\\&quot;); // To make sure we read from root dir
done = findfirst(&quot;*.&quot;,&amp;ffblk,faDirectory);
while (!done)
{
// only store directories, ignoring &quot;.&quot; and &quot;..&quot;
char *impline;
impline = ffblk.ff_name;
if(impline[0]!='.') // another problem is that full pathname is not listed!!!
Memo1->Lines->Add(impline);
done = findnext(&amp;ffblk);
}
}
//---------

A simple folder structure I am trying to use to find a solution!! It's gives more pain then help! {=P

C: |-Windows-|-System32-|-Folder
| |-Command-|Folder
|
|-Program files-|-ACDSee
| |-Internet Explorer-|-PLUGINS
|-Backup-|-System-|-Folder 1
| | |-Folder 2
| | |-Folder 3

Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
 
There might be any easy way if you want to display on screen the directories. Look at the VCL components TDriveComboBox, TDirectoryListBox, and TFilterCombo.

What you are doing above is correct so far. However, you need to use the full wildcard &quot;*.*&quot; and then call a Windows API named WIN32_FIND_DATA. When the FILE_ATTRIBUTE_DIRECTORY bit is set, then you have a directory.

I would suggest the book Borland C++ Builder: The Definitve Problem-Solver by Miano, Cabanski, and Howe. Howe has a web site, that has some of the books examples on site. Also check out the links at his site.

Failing that, e-mail me at cottingham@msinets.com and I can send you some examples.
James P. Cottingham
 
I do not wish to show the folder(s) on screen... I only want to search a folder(inc. subdirs) for files(*.*) and then save a list of folder Contents to a file, a friend of my brother wants such a program to read a CD!

If I am to use the API WIN32_FIND_DATA I need to know how it works!! How to use it!!
I would appreciate if you could help me with that!

Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
 
I haven't forgotten about you. I am putting together an example but I haven't had much spare time. Please stand by.
James P. Cottingham
 
Sorry this has taken me so long. I've been very busy, as usual, so I haven't had much time to research this out. I'm going to show you a part of a program that I wrote. Then I'm going to add some elements that I haven't test yet. I'm doing this on my lunch hour so I don't have much time. I was going to do two programs. One the way I'm showing you and the other using WIN32_FIND_DATA but I don't have time. So here goes:
Code:
struct ffblk ff;	// prototype in dir.h

int done, direct, subdirect;

// get directories in question
done = findfirst(&quot;C:\\*.&quot;, &amp;ff, FA_DIREC);
while (!done)
     {
          // don't include current directory or root directory
          direct = strcmp(ff.ff_name, &quot;.&quot;);
          subdirect = strcmp(ff.ff_name, &quot;..&quot;);

          if (direct != 0 &amp;&amp; subdirect != 0)
          {
	cout << &quot;Found File: &quot; << ff.ff_name << endl;
              AnsiString SFile = ff.ff_name; // I'm not certain if this works
              AnsiString SPath = ExtractFilePath(SFile); // If the above work, this will
          }
	
     // get next directoy
     done = findnext(&amp;ff);
}

AnsiString and ExtractFilePath are automatically included with vcl.h so there is nothing to include manually. ExtractFilePath is part of the Sysutils unit and is poorly documented but has a lot of useful functions. AnsiString is well documented.

The only thing I can't remember right know is if you can set SFile to ff.ff_name directly or if you have to convert one or the other first, maybe with .c_str.

The other thing I notice is that you used faDirectory in findfirst. I used FA_DIRECT.

Good luck. My lunch time is over.
James P. Cottingham
 
Thanks a lot!

You know what, I actually made an application that searches subdirectories! :) Neat or what?
Though the code is somewhat stirred up, it actually works.
Perhaps you will find a flaw or two(hundred =); and if you do find something that can be improved, please let me know!
Here is the main search code: (It uses A TListBox and a TRichEdit and a TButton)
[tt]AnsiString curr_path; // the path to search in
void __fastcall TForm1::Button1Click(TObject *Sender)
{
RichEdit1->Lines->Clear(); // clear TRichEdit
ListBox1->Items->Clear(); // clear TListBox
int direct, subdirect;
ListBox1->Items->Add(“C:”); // Add root path, the first path we will search
while(ListBox1->Items->Count>0) // While we have a folder to search
{ // get new path to search,
curr_path = ListBox1->Items->Strings[0]+&quot;\\&quot;;
done = findfirst((AnsiString(curr_path)+&quot;*.*&quot;).c_str(), &amp;ffblk,faDirectory);
while (!done)
{ // compare two strings
direct = strcmp(ffblk.ff_name, &quot;.&quot;);
subdirect = strcmp(ffblk.ff_name, &quot;..&quot;);
if (direct != 0 &amp;&amp; subdirect != 0 &amp;&amp; ffblk.ff_attrib==FA_DIREC)
// add path to TListBox
ListBox1->Items->Insert(1, (AnsiString(curr_path) + AnsiString(ffblk.ff_name)) );
if(direct != 0 &amp;&amp; subdirect != 0 &amp;&amp; ffblk.ff_attrib!=FA_DIREC)
// add path + filename to TRichEdit
RichEdit1->Lines->Add( curr_path+ffblk.ff_name );
done = findnext(&amp;ffblk); // Find next file or folder
}
ListBox1->Items->Delete(0); // delete previously searched folder from TListBox
}
}
[/tt]


Thanks for helping and inspiring!
Martin

P.S I appreciate you taking time to help me whenever needed! It has been very helpful! D.S

Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
 
Your welcome. Glad I could be of some (little?) help. Why not make a FAQ in this thread with your code? you may have to make it a little more generic but I suspect more people will be looking for something similar.

James P. Cottingham
 
How do I make a FAQ out of this thread?!
I would more then gladly supply others with it when modified a bit and more efficient (of course)!
Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
 
What I did was create the FAQ in an editor. Then went to this forum, opened up the FAQ, created a new thread, and pasted the FAQ from the editor to the forum's FAQ. James P. Cottingham
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top