|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Simple "Not" Match?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. xeroxero wrote:
> I have code that recursively traverses a directory and writes to a Start from here:> 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? 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 Great, thanks!
On Wed, 1 Nov 2006 19:54:17 -0800, "Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam> wrote: Show quote >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; > > // this file matches - do something with it. > } > } >} > >-cd >
Show quote
"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam> Unless I miss my guess this won't actually skip directories that match...wrote in message news:%23Ik3SKj$GHA.4676@TK2MSFTNGP04.phx.gbl... > 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; Show quote > > // this file matches - do something with it. > } > } > } > > -cd > > Ben Voigt wrote:
Show quote > "Carl Daniel [VC++ MVP]" Yes, that's true - I missed the OPs statement that he wanted to skip files > <cpdaniel_remove_this_and_nospam@mvps.org.nospam> wrote in message > news:%23Ik3SKj$GHA.4676@TK2MSFTNGP04.phx.gbl... >> 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... 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 |
|||||||||||||||||||||||