Discussion:
Search in Sub Folder
(too old to reply)
LearnJoo
2007-11-10 21:58:08 UTC
Permalink
Hi
in this Code below, what change should I make for searching sub folders
also?

AnsiString MyStr="*.txt";
ListBox1->Clear();
ListBox1->Items->Add("All Text Files");

HANDLE hFile = NULL;
WIN32_FIND_DATA wfs = {NULL};
hFile=FindFirstFile(MyStr.c_str(),&wfs);
do{
if(INVALID_HANDLE_VALUE == hFile){NULL;}
else{
ListBox1->Items->Add(wfs.cFileName);
}
}
while(FindNextFile(hFile,&wfs)!=false);


Plus ! I want to keep full path in Listbox,
waiting for a help
Thanks
chenzero
2007-11-12 15:31:29 UTC
Permalink
Hi,
actually, this is a classic algorithm in "data structure",
Following is the non-recursive algorithm, there is also
recursive algorithm.
//---------------------------------------------------------

#include <stack>
using namespace std;

AnsiString dir= "****";
stack<AnsiString> stk;
WIN32_FIND_DATA wfs = {NULL};

stk.push(dir);
while (!stk.empty()) {
dir = stk.pop();

hFile=FindFirstFile(dir.c_str(),&wfs);
do{
if(INVALID_HANDLE_VALUE == hFile){
break;
}

if ((wfs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)!=0 ) { // a dir
stk.push(dir + wfs.cFileName);
}
else { // it's a file
ListBox1->Items->Add(wfs.cFileName);
}
}
while(FindNextFile(hFile,&wfs)!=false);
}
Post by LearnJoo
Hi
in this Code below, what change should I make for searching sub folders
also?
AnsiString MyStr="*.txt";
ListBox1->Clear();
ListBox1->Items->Add("All Text Files");
HANDLE hFile = NULL;
WIN32_FIND_DATA wfs = {NULL};
hFile=FindFirstFile(MyStr.c_str(),&wfs);
do{
if(INVALID_HANDLE_VALUE == hFile){NULL;}
else{
ListBox1->Items->Add(wfs.cFileName);
}
}
while(FindNextFile(hFile,&wfs)!=false);
Plus ! I want to keep full path in Listbox,
waiting for a help
Thanks
Ed Mulroy [TeamB]
2007-11-12 18:15:17 UTC
Permalink
Oops, I think there is are a couple of typos in there.

Instead of:
while (!stk.empty()) {
dir = stk.pop();

Try this intstead:
while (!stk.empty()) {
dir = stk.top();
stk.pop(); // stk.pop() return type is void


Instead of:
if ((wfs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)!=0 ) { // a dir
stk.push(dir + wfs.cFileName);

Try this instead:
if ((wfs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)!=0 ) { // a dir
// Ignore "." and ".." directory names
if ((wfs.cFileName[0] != '.') ||
(wfs.cFileName[1] != '.') (wfs.cFileName[1] != '\.'))
stk.push(dir + wfs.cFileName);

. Ed
Post by chenzero
actually, this is a classic algorithm in "data structure",
Following is the non-recursive algorithm, there is also
recursive algorithm.
//---------------------------------------------------------
#include <stack>
using namespace std;
AnsiString dir= "****";
stack<AnsiString> stk;
WIN32_FIND_DATA wfs = {NULL};
stk.push(dir);
while (!stk.empty()) {
dir = stk.pop();
hFile=FindFirstFile(dir.c_str(),&wfs);
do{
if(INVALID_HANDLE_VALUE == hFile){
break;
}
if ((wfs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)!=0 ) { // a dir
stk.push(dir + wfs.cFileName);
}
else { // it's a file
ListBox1->Items->Add(wfs.cFileName);
}
}
while(FindNextFile(hFile,&wfs)!=false);
}
Post by LearnJoo
Hi
in this Code below, what change should I make for searching sub folders
also?
AnsiString MyStr="*.txt";
ListBox1->Clear();
ListBox1->Items->Add("All Text Files");
HANDLE hFile = NULL;
WIN32_FIND_DATA wfs = {NULL};
hFile=FindFirstFile(MyStr.c_str(),&wfs);
do{
if(INVALID_HANDLE_VALUE == hFile){NULL;}
else{
ListBox1->Items->Add(wfs.cFileName);
}
}
while(FindNextFile(hFile,&wfs)!=false);
Plus ! I want to keep full path in Listbox,
waiting for a help
Remy Lebeau (TeamB)
2007-11-12 18:11:18 UTC
Permalink
Post by LearnJoo
in this Code below, what change should I make for searching
sub folders also?
You need to change your mask to include all items, and then you need to look
at the attributes that WIn32_FIND_DATA provides so you can know whether a
given item is a file or a folder. For example:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
ListBox1->Clear();
ListBox1->Items->Add("All Text Files");

FindAllTextFiles("C:\\some folder");
}

void __fastcall TForm1::FindAllTextFiles(const AnsiString &Folder)
{
AnsiString asFolder = IncludeTrailingBackslash(Folder);

WIN32_FIND_DATA wfs;
HANDLE hFile = FindFirstFile((asFolder + "*.*").c_str(), &wfs);
if( hFile != INVALID_HANDLE_VALUE )
{
do
{

AnsiString asFileName = wfs.cFileName;

if( wfs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
if( (asFileName != ".") && (asFileName != "..") )
FindAllTextFiles(asFolder + asFileName);
}
else
{
if( AnsiSameText(ExtractFileExt(asFileName), ".txt") )
ListBox1->Items->Add(asFolder + asFileName);
}
}
while( FindNextFile(hFile, &wfs) );
FindClose(hFile);
}
}


Gambit
Continue reading on narkive:
Loading...