|
-
Jul 21st, 2007, 04:30 AM
#1
Thread Starter
Hyperactive Member
[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...
*****************
VB6,PHP,VS 2005
-
Jul 21st, 2007, 03:18 PM
#2
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();
}
}
-
Jul 21st, 2007, 04:48 PM
#3
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.
-
Jul 22nd, 2007, 07:38 PM
#4
Thread Starter
Hyperactive Member
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();
}
}
*****************
VB6,PHP,VS 2005
-
Jul 23rd, 2007, 03:37 AM
#5
Thread Starter
Hyperactive Member
Re: exclude folder or files
hello!
just one more question i just want also to exlude the files *.avi.
how?
*****************
VB6,PHP,VS 2005
-
Jul 23rd, 2007, 08:44 AM
#6
Re: exclude folder or files
I'm no Regex expert, but this one seems to match:
-
Jul 23rd, 2007, 06:33 PM
#7
Thread Starter
Hyperactive Member
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...
*****************
VB6,PHP,VS 2005
-
Jul 23rd, 2007, 10:31 PM
#8
Addicted Member
Re: exclude folder or files
 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...
-
Jul 24th, 2007, 01:32 AM
#9
Thread Starter
Hyperactive Member
Re: exclude folder or files
"\\w[avi]$"
any explanation? what are the meaning?
1. w
2. [avi] i know is the extension name
3. $
*****************
VB6,PHP,VS 2005
-
Jul 24th, 2007, 08:13 AM
#10
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
-
Jul 24th, 2007, 07:18 PM
#11
Thread Starter
Hyperactive Member
Re: exclude folder or files
tnx a lot for the nfo.
*****************
VB6,PHP,VS 2005
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|