Re: Simple "Not" Match?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Ben Voigt wrote:
"Carl Daniel [VC++ MVP]"
<cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx> wrote in message
news:%23Ik3SKj$GHA.4676@xxxxxxxxxxxxxxxxxxxxxxx
xeroxero wrote:
I have code that recursively traverses a directory and writes to a
file. I would like to ignore any directory or file that matches a
pattern that is in a string array ( '*.obj' , 'doc*.*' ). Can anyone
show an example of how to do that?

Start from here:

http://www.codeproject.com/cs/files/FileSystemEnumerator.asp

Then use a regular expression to reject items that you don't want to
include. Something like this:

using System.Text.RegularExpressions;

void SomeMethod(string path)
{
Regex reject = new Regex(@"^(.*\.obj)|(doc.*)$");

using (FileSystemEnumerator fse = new FileSystemEnumerator(path,
"*",true))
{
foreach (FileInfo fi in fse.Matches())
{
if (reject.IsMatch(fi.Name))
continue;

Unless I miss my guess this won't actually skip directories that
match...

Yes, that's true - I missed the OPs statement that he wanted to skip files
OR directories that match. To be 100% accurate applying to individual
directories:

replace the simple if (reject.IsMatch(fi.Name))

with something like...

string[] parts = fi.FullName.Split(Path.PathSeparator);
if (parts.Length < 2)
continue;
string[] names = parts[1].Split(Path.DirectorySeparatorChar);
bool matched = false;
foreach (string name in names)
{
if (reject.IsMatch(name))
{
matched = true;
break;
}
}
if (matched)
continue;

.... I'm sure with enough fiddling a regex could be constructed that handles
it all. I'm not sure using such a regex would actually be faster since it
could involve a lot of backtracking during regex matching which might take
as much time as just splitting the string up.

Of course, the best way to deal with directories in this case would be to
modify FileSystemEnumerator to also accept a list of directory
names/filespecs to exclude, so the entire visitation of a matching directory
tree would be pruned out.

-cd


.



Relevant Pages

  • What could potentially be wrong in this script?
    ... I am writing a Perl script to check that dependency files exist that ... opening it, and then based on the type of file, saving a regex to ... foreach my $files ... The index call isn't working on this bizarre string. ...
    (comp.lang.perl.misc)
  • Re: Concatenating regular exprs in a grep call
    ... Further you put a code construct in a string (so not a Perl regex). ... foreach my $r { ...
    (comp.lang.perl.misc)
  • Re: Need Regex for phone number
    ... l> txt string regardless of how it was formatted. ... I have regex that identifies a ... strings and that integer comparison is faster. ... foreach my $input ...
    (comp.lang.perl.misc)
  • Re: RegEx how do I do unique?
    ... RegEx how do I do unique? ... If this is for an Excel spreadsheet, ... > foreach ... to find the unique occurrences of words in a string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Fastest way to search a string for the occurance of a word??
    ... but the OP's question was what's the "Fastest way to search a string ... in all the tests I did here, the Regex was by far superior. ... However, of course, if you've got new regular expressions all ... Sure - but just that extra Match object could be relevant if the search ...
    (microsoft.public.dotnet.languages.csharp)