Results 1 to 11 of 11

Thread: [RESOLVED] exclude folder or files

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Resolved [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

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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();
                    }
                }

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    Re: exclude folder or files

    hello!
    just one more question i just want also to exlude the files *.avi.

    how?
    *****************
    VB6,PHP,VS 2005

  6. #6
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: exclude folder or files

    I'm no Regex expert, but this one seems to match:
    Code:
    \w[avi]$

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    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

  8. #8
    Addicted Member effekt26's Avatar
    Join Date
    Nov 2006
    Posts
    138

    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...

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    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

  10. #10
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2006
    Location
    /root/usr/local/bin
    Posts
    476

    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
  •  



Click Here to Expand Forum to Full Width