[RESOLVED] exclude folder or files
Code:
foreach (string filepath in System.IO.Directory.GetFiles(@"C:\Documents and Settings\user", "*.*", System.IO.SearchOption.AllDirectories))
{
FileInfo fileSize = new FileInfo(filepath);
if (fileSize.Length > 25000000)
{
File.SetAttributes(filepath, FileAttributes.Normal);
listBox1.Items.Add(filepath.ToString() + "-" + fileSize.Length.ToString());
}
label1.Text= listBox1.Items.Count.ToString();
}
above code will search all files including sub directories
but i want to EXCLUDE the file or folder below:
C:\Documents and Settings\user\Local Settings\Application Data\Microsoft\Outlook\*.*
how? tnx in advance...
Re: exclude folder or files
I imagine that there is a better way, but I don't know what it is. However, you could do something like this:
Code:
string exDir = @"C:\\Documents and Settings\\user\\Local Settings\\Application Data\\Microsoft\\Outlook\\*";
foreach (string filepath in System.IO.Directory.GetFiles(@"C:\Documents and Settings\user", "*.*", System.IO.SearchOption.AllDirectories))
{
if (!System.Text.RegularExpressions.Regex.IsMatch(filepath, exDir))
{
FileInfo fileSize = new FileInfo(filepath);
if (fileSize.Length > 25000000)
{
File.SetAttributes(filepath, FileAttributes.Normal);
listBox1.Items.Add(filepath.ToString() + "-" + fileSize.Length.ToString());
}
label1.Text = listBox1.Items.Count.ToString();
}
}
Re: exclude folder or files
Whether you use a regex or not, the only way to do what you ask is to specify that path as one to exclude and then test the path of each folder against it as you go.
Re: exclude folder or files
Code:
!System.Text.RegularExpressions.Regex.IsMatch(filepath, exDir))
tnx to the RegularExpression now its working...
HTML Code:
Uname = System.Windows.Forms.SystemInformation.UserName;
string exDir1 = @"C:\\Documents and Settings\\" + Uname + @"\\Local Settings\\Application Data\\Microsoft\\Outlook\\*";
string exDir2 = @"C:\\Documents and Settings\\" + Uname + @"\\Application Data\\Microsoft\\Outlook\\*";
string exDir3 = @"C:\\Documents and Settings\\" + Uname + @"\\Local Settings\\Application Data\\Thunderbird\\*";
string exDir4 = @"C:\\Documents and Settings\\" + Uname + @"\\Application Data\\Thunderbird\\*";
foreach (string filepath in System.IO.Directory.GetFiles(@"C:\Documents and Settings\" + Uname, "*.*", System.IO.SearchOption.AllDirectories))
{
if (!System.Text.RegularExpressions.Regex.IsMatch(filepath, exDir1) && !System.Text.RegularExpressions.Regex.IsMatch(filepath, exDir2) && !System.Text.RegularExpressions.Regex.IsMatch(filepath, exDir3) && !System.Text.RegularExpressions.Regex.IsMatch(filepath, exDir4))
{
FileInfo fileSize = new FileInfo(filepath);
if (fileSize.Length > 25000000)
{
File.SetAttributes(filepath, FileAttributes.Normal);
listBox1.Items.Add(filepath.ToString() + "-" + fileSize.Length.ToString());
}
label1.Text = listBox1.Items.Count.ToString();
}
}
Re: exclude folder or files
hello!
just one more question i just want also to exlude the files *.avi.
how?
Re: exclude folder or files
I'm no Regex expert, but this one seems to match:
Re: exclude folder or files
"\\w[avi]$" this works great, but i wonder why they implement this such kind of
function... i can say that microsoft can create like this "*.avi" it is much easier...
tnx for your help...
Re: exclude folder or files
Quote:
Originally Posted by basti42
"\\w[avi]$" this works great, but i wonder why they implement this such kind of
function... i can say that microsoft can create like this "*.avi" it is much easier...
tnx for your help...
That is how regular expressions work...they are an extremly powerful tool...and can match against any possible string you need, which it why they look so complicated...
however, im guessing the more you learn to use them and work with them, the simpler they become, just like most things...
Re: exclude folder or files
"\\w[avi]$"
any explanation? what are the meaning?
1. w
2. [avi] i know is the extension name
3. $
Re: exclude folder or files
\w matches any word character.
$ specifies that the match must occur at the end of the string.
Here are all of the explanations:
http://msdn2.microsoft.com/en-us/lib...fc(vs.80).aspx
Re: exclude folder or files
tnx a lot for the nfo. :)