🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
4 messages
4 total messages Started by Sören Zimmermann Mon, 30 Jun 2008 13:28
extended file search
#166
Author: Sören Zimmermann
Date: Mon, 30 Jun 2008 13:28
38 lines
1885 bytes
Hi all,

I've got a problem and don't know how to do best:

I need to delete some files from a directory (including subdirs). The 
joke is, that we need a complex file definition which file to delete and 
which one not. We need include files and exclude files, account for file 
size etc.

A small example:


TStringList *IncludedFiles, *ExcludedFiles;
IncludedFiles->Add("*.bmp");
IncludedFiles->Add("*.gif");
ExcludedFiles->Add("Default.*");

When executing the code (e.g. a class or component) in a directory 
containing the files
  (1) Default.bmp
  (2) foo.bmp
  (3) Default.gif
  (4) HelloYou.gif
  (5) YetAnotherFile.txt

only the files (2) and (4) should be affected.

So what I need is an idea of code (or maybe some existing component) of 
how to create a list of all files matching the definitions.

Of course I could use FindFirstFile/FindNextFile, but then I will only 
get a list of files matching my include files. How can I filter out my 
exclude files, esp. when they contain wildcards?


Thanks for some ideas,

S�ren
Re: extended file search
#167
Author: Helmut Giese
Date: Mon, 30 Jun 2008 15:34
27 lines
1995 bytes
On Mon, 30 Jun 2008 15:28:19 +0200, S�ren Zimmermann
<soeren.zimmermann@sef-softec.de> wrote:

Hi S�ren,
>Of course I could use FindFirstFile/FindNextFile, but then I will only 
>get a list of files matching my include files. How can I filter out my 
>exclude files, esp. when they contain wildcards?
I think you will have to use FindFirstFile/FindNextFile (or some
higher level component which will use these functions internally).

Here's how I would do it if I wouldn't want to think too hard about
it:
Step 1) Build a list containing all _excluded_ files. That is use
FindFirstFile/FindNextFile on all "patterns to exclude".
Step 2) Run FindFirstFile/FindNextFile for each pattern you want
included. If any file is part of the list built in step (1), ignore
it, else delete it.

Regarding performance it is probably faster to
- get a list of _all_ files and then for each of them
- check if it matches an 'exclude file' (if so continue) or
- if it matches an 'include file'

But it's more programming work, so I'd start out with the easy way
outlined above
HTH
Helmut Giese
Re: extended file search
#169
Author: Remy Lebeau (TeamB)
Date: Mon, 30 Jun 2008 16:39
72 lines
3000 bytes
"S�ren Zimmermann" <soeren.zimmermann@sef-softec.de> wrote in message 
news:4868dffe$1@newsgroups.borland.com...

> So what I need is an idea of code (or maybe some existing component)
> of how to create a list of all files matching the definitions.

The VCL has a MatchesMask() function that you can use for that, ie:

    #include <Masks.hpp>

    bool __fastcall IsAMatch(const AnsiString &FileName, TStrings *Masks)
    {
        for(int i = 0; i < Masks->Count; ++i)
        {
            if( MatchesMask(FileName, Masks->Strings[i]) )
                return true;
        }
        return false;
    }

    void __fastcall DeleteFiles(const AnsiString &Directory, TStrings 
*IncludedFiles, TStrings *ExcludedFiles)
    {
        AnsiString tmpDir = IncludeTrailingBackslash(Directory);

        TSearchRec sr;
        if( FindFirst(tmpDir + "*.*", faAnyFile, sr) == 0 )
        {
            try
            {
                do
                {
                    if( sr.Attr & faDirectory )
                        DeleteFiles(tmpDir + sr.Name, IncludedFiles, 
ExcludedFiles);

                    else if( IsAMatch(sr.Name, IncludedFiles) && 
!IsAMatch(sr.Name, ExcludedFiles) )
                        DeleteFile(tmpDir + sr.Name);
                }
                while( FindNext(sr) == 0 );
            }
            __finally
            {
                FindClose(sr);
            }
        }
    }

    {
        TStringList *IncludedFiles, *ExcludedFiles;
        ...
        IncludedFiles->Add("*.bmp");
        IncludedFiles->Add("*.gif");
        ExcludedFiles->Add("Default.*");
        ...
        DeleteFiles("C:\\Some directory", IncludedFiles, ExcludedFiles);
    }

> Of course I could use FindFirstFile/FindNextFile

You will have to do that anyway.

> but then I will only get a list of files matching my include files.

Only if you rely on FindFirstFile()'s pattern matching.  Instead, you can 
pass it "*.*" to get all files, and then match them to your search lists 
manually.


Gambit 
Re: extended file search
#209
Author: Sören Zimmermann
Date: Fri, 04 Jul 2008 15:36
5 lines
1083 bytes
Yes, the MatchesMask() function was exactly what I was looking for. I 
implemented the code and it works fine.

Thanks very much,
S�ren
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