🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #11383

Re: Search directories for a file mask problem...

#11383
From: Remy Lebeau (TeamB)
Date: Fri, 06 Feb 2004 22:11
65 lines
3267 bytes
"Martin C" <martinc@aol.com> wrote in message
news:40240050@newsgroups.borland.com...

> when I tried to change the file mask to something like
> (*.gif) for example it didn't return any file found.

That is because you are not paying attention to wildcards at all.  You are
trying to do a straight 1-to-1 comparison on something that is not
comparable that way to begin with.  "somefile.gif" and "*.gif" cannot be
directly compared because the "*" does not appear in the actual filename.
It is a placeholder that needs to be parsed and resolved dynamically.  That
is why I changed your earlier code to use MatchesMask() instead, as it
handles wildcard matching.  If you do not have MatchsMask() available in
your version of BCB, then you will just have to do the parsing manually.  If
all you are actually interested in is file extensions and nothing else, then
do not specify "*" at all, and then you can use ExtractFileExt() for the
comparison, ie:

    SearchForFile("C:\\", ".gif");

    bool __fastcall TForm1::SearchForFile(AnsiString Folder, const
AnsiString &MaskToFind)
    {
        TSearchRec sr;
        bool returnedval = false;

        Folder = IncludeTrailingBackslash(Folder);
        if( FindFirst(Folder + "*.*", faAnyFile, sr) == 0 )
        {
            do
            {
                if( (sr.Name != ".")  && (sr.Name != "..") )
                {
                    if(sr.Attr & faDirectory)
                        returnedval = SearchForFile(Folder + sr.Name,
MaskToFind);
                    else
                    {
                         if( AnsiSameText(ExtractFileExt(sr.Name),
MaskToFind) )
                        {
                            ListBox1->Items->Add(sr.Name);
                            returnedval = true;
                        }
                    }
                }
            }
            while( FindNext(sr) == 0 );
            FindClose(sr);
        }

        return returnedval;
    }

> P.S. I replaced MatchesMask function with AnsiString Pos function
> as I had problems compiling the code with MatchesMask (returns
> error call to undefined function).

Did you include its header file?

    #include <Masks.hpp>


Gambit
Technical Details
Message-ID: <40240f75$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <402301fc@newsgroups.borland.com> <40232de8$1@newsgroups.borland.com> <40240050@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,b023f6f11f20de80
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-02-06 14:10:16 PST
From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <402301fc@newsgroups.borland.com> <40232de8$1@newsgroups.borland.com> <40240050@newsgroups.borland.com>
Subject: Re: Search directories for a file mask problem...
Date: Fri, 6 Feb 2004 14:11:58 -0800
Lines: 67
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 67.22.138.253
Message-ID: <40240f75$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1076105077 67.22.138.253 (6 Feb 2004 14:04:37 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail