CFileFind 
Author Message
 CFileFind

Hi,

I was trying to find whether a folder has any files. I
used

CFileFind finder;
CString cstrFilename = ss + "\\*.*";  //ss is directory
bool bFile1   = finder.FindFile(cstrFilename);
bool bFile2   = finder.FindNextFile();
CString strName1 = finder.GetFileName();
bool bFile3   = finder.FindNextFile();
CString strName2 = finder.GetFileName();
bool bFile4   = finder.FindNextFile();
CString strName3 = finder.GetFileName();

even if the folder is empty, bFile1 and bFile2 will
return "true"
the strName1 will be "." and strName2 will be ".."
so I can only know whether the folder really has files
by judging bFile4 or strName3.

Do I have to call "finder.FindNextFile()" three times
or there is a better way to do this.

Thanks a lot.
jim



Sun, 11 Sep 2005 20:19:48 GMT  
 CFileFind
Use a loop and test for folders with CFileFind::IsDirectory() (see the
documentation for an example) and/or CFileFind::IsDots().


Quote:
> Hi,

> I was trying to find whether a folder has any files. I
> used

> CFileFind finder;
> CString cstrFilename = ss + "\\*.*";  //ss is directory
> bool bFile1   = finder.FindFile(cstrFilename);
> bool bFile2   = finder.FindNextFile();
> CString strName1 = finder.GetFileName();
> bool bFile3   = finder.FindNextFile();
> CString strName2 = finder.GetFileName();
> bool bFile4   = finder.FindNextFile();
> CString strName3 = finder.GetFileName();

> even if the folder is empty, bFile1 and bFile2 will
> return "true"
> the strName1 will be "." and strName2 will be ".."
> so I can only know whether the folder really has files
> by judging bFile4 or strName3.

> Do I have to call "finder.FindNextFile()" three times
> or there is a better way to do this.

> Thanks a lot.
> jim



Sun, 11 Sep 2005 21:26:48 GMT  
 CFileFind


Quote:
> Do I have to call "finder.FindNextFile()" three times
> or there is a better way to do this.

AFAIK, there is not better way.
I don't like the windows CFileFind system at all. It seems a little silly to
me to provide functionality in this way. Surely handeling the "." and ".."
directories are always needed, also recursing sub directories & filters
would be useful.
I can't understand why this functionality isn't in MFC.
I have had to write my own class to do this. My class is below if you want
it. Just use the public member function FindFiles and pass it relevent
paramaters and it should work.

Note: Attaching files to messages.
I know it's not a good idea to attach files to news group postings so I have
text copied the two files below.
I understand why for large files like programs and pictures I myself access
the group on a modem.
But, is there any reason why no one seems to attach .h and .cpp files to
their postings here?

Cheers
Rob

// RJMFileFind2.h: interface for the RJMFileFind2 class.
//
//////////////////////////////////////////////////////////////////////

#if
!defined(AFX_RJMFILEFIND2_H__C74CA8E0_41F2_11D7_8C28_00C0F0455C98__INCLUDED_)
#define AFX_RJMFILEFIND2_H__C74CA8E0_41F2_11D7_8C28_00C0F0455C98__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class RJMFileFind2
{
private:
        void SearchSubDir(CString DIR,CStringArray *res);
        void PhraseFilter(CString filter);
        CStringArray Acc_Exts_Left;
        CStringArray Acc_Exts_Right;
        CFileFind* m_pFinder;
        bool PassFilter(CString t);
public:
        void FindFiles(bool sub, CString dir, CString filter, CStringArray *res);
        RJMFileFind2();
        virtual ~RJMFileFind2();

Quote:
};

#endif //
!defined(AFX_RJMFILEFIND2_H__C74CA8E0_41F2_11D7_8C28_00C0F0455C98__INCLUDED_)

// RJMFileFind2.cpp: implementation of the RJMFileFind2 class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "bob_photo.h"
#include "RJMFileFind2.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

RJMFileFind2::RJMFileFind2()
{
        m_pFinder = NULL;

Quote:
}

RJMFileFind2::~RJMFileFind2()
{
        if (m_pFinder) delete m_pFinder;

Quote:
}

void RJMFileFind2::FindFiles(bool sub, CString dir, CString filter,
CStringArray *res)
{
        //Searches dir for files and loads into res array
        PhraseFilter(filter);
        res->RemoveAll();

        if (m_pFinder) delete m_pFinder;
        m_pFinder = new CFileFind;

        if (!(dir.Right(1) == _T("\\"))) {
                dir = dir + _T("\\");
        };

        BOOL bWorking = m_pFinder->FindFile(dir + _T("*.*"));
        CString t;
        bool gosub = false;
        while (bWorking) {
                bWorking = m_pFinder->FindNextFile();
                t = m_pFinder->GetFileName();
                if (m_pFinder->IsDirectory()) {
                        gosub = true;
                        if (t == _T(".")) gosub = false;
                        if (t == _T("..")) gosub = false;

                        if (sub) if (gosub) {
                                SearchSubDir(dir + m_pFinder->GetFileName(),res);
                        };
                } else {
                        if (PassFilter(t)) {
                                t = dir + t;
                                res->Add(t);
                        };
                };
        }

Quote:
}

bool RJMFileFind2::PassFilter(CString t)
{
        //checks if t passes the filter

        int c = t.Find(_T("."),0);
        CString start = t.Left(c);
        CString end = t.Right(t.GetLength() - (c+1));
        start.MakeUpper();
        end.MakeUpper();
        CString l;
        CString r;
        for (c=0;c<Acc_Exts_Left.GetSize();c++) {
                l = Acc_Exts_Left.GetAt(c);
                r = Acc_Exts_Right.GetAt(c);
                if (l == _T("*")) {
                        if (r == _T("*")) return true;
                        //only right matters
                        if (r == end) return true;
                } else {
                        //left matters
                        if (l == start) {
                                if (r == _T("*")) {
                                        //only left matters
                                        return true;
                                } else {
                                        //they both matter
                                        if (r == start) return true;
                                };
                        };
                };
        };
        return false;

Quote:
}

void RJMFileFind2::PhraseFilter(CString filter)
{
        Acc_Exts_Left.RemoveAll();
        Acc_Exts_Right.RemoveAll();
        //Split string on ";"
        CString t;
        int c = filter.Find(_T(";"),0);
        while (!(c ==-1)) {  //while
                //valid c value
                t = filter.Left(c);
                filter = filter.Right(filter.GetLength() - (c+1));              
                Acc_Exts_Left.Add(t);
                c = filter.Find(_T(";"),0);  
        };
        Acc_Exts_Left.Add(filter);
        Acc_Exts_Right.SetSize(Acc_Exts_Left.GetSize());
        int d;
        CString l = _T("");
        CString r = _T("");
        for (c=0;c<Acc_Exts_Left.GetSize();c++) {
                t = Acc_Exts_Left.GetAt(c);
                d = t.Find(_T("."),0);
                l = t.Left(d);
                r = t.Right(t.GetLength() - (d+1));
                l.MakeUpper();
                r.MakeUpper();
                Acc_Exts_Left.SetAt(c,l);
                Acc_Exts_Right.SetAt(c,r);
        };

Quote:
}

void RJMFileFind2::SearchSubDir(CString DIR, CStringArray *res)
{
//      TRACE(_T("SearchSubDir start DIR:"));
//      TRACE(DIR);
//      TRACE(_T(":\n"));

        DIR += _T("\\");
        CFileFind finder;
        BOOL bWorking = finder.FindFile(DIR + _T("*.*"));
        CString t;
        bool gosub = false;
        while (bWorking) {
                bWorking = finder.FindNextFile();
                t = finder.GetFileName();
                if (finder.IsDirectory()) {
                        gosub = true;
                        if (t == _T(".")) gosub = false;
                        if (t == _T("..")) gosub = false;

                        if (gosub) SearchSubDir(DIR + finder.GetFileName(),res);
                } else {
//                      TRACE(_T("File Found:"));
//                      TRACE(t);
//                      TRACE(_T(":\t"));
                        if (PassFilter(t)) {
                                t = DIR + t;
                                res->Add(t);
//                              TRACE(_T("PASS"));
                        };
//                      TRACE(_T("\n"));
                };
        }

//      TRACE(_T("SearchSubDir end\n"));

Quote:
}



Mon, 12 Sep 2005 23:58:20 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Problem with CFileFind

2. Problems using CFileFind

3. CFileFind, how to find the all???

4. CFileFind.

5. CFileFind and Long-File-Names?

6. CFileFind - weirdity..

7. How those the CFileFind work ?

8. CFileFind question

9. CFileFind

10. CFileFind fails on networked drives running on Win2K/NT service - Japanese

11. CFileFind

12. CFileFind * HELP PLEASE *

 

 
Powered by phpBB® Forum Software