Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / November 2006

Tip: Looking for answers? Try searching our database.

Simple "Not" Match?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
xeroxero - 01 Nov 2006 22:44 GMT
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?

Thanks.
Carl Daniel [VC++ MVP] - 02 Nov 2006 04:54 GMT
> 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;

           // this file matches - do something with it.
       }
   }
}

-cd
xeroxero - 02 Nov 2006 17:23 GMT
Great, thanks!

>> 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
[quoted text clipped - 28 lines]
>
>-cd
Ben Voigt - 03 Nov 2006 15:16 GMT
>> 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
[quoted text clipped - 21 lines]
>            if (reject.IsMatch(fi.Name))
>                continue;

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

>            // this file matches - do something with it.
>        }
>    }
> }
>
> -cd
Carl Daniel [VC++ MVP] - 04 Nov 2006 16:19 GMT
>>> 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
[quoted text clipped - 24 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.