Results 1 to 8 of 8

Thread: URGENT : How to find total files + folder

  1. #1

    Thread Starter
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Exclamation URGENT : How to find total files + folder

    hi guys is anyone have an idea how to find total no of files and folder under perticular path..


    I have thought of
    VB Code:
    1. Directory.GetFiles("..").Length
    but how to use it in deep hierarchy...


    thanks

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

    Re: URGENT : How to find total files + folder

    Are you using .NET 2.0? If so then Directory.GetFiles and .GetDirectories each have a new overload that allows you to specify that you want to search all subfolders too. If not then you'll have to write your own recursive method to search subfolders, which is not difficult. Please specify which version you're using in future.
    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

  3. #3

    Thread Starter
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Re: URGENT : How to find total files + folder

    Well Im using .NET 2003. However, Im intended to move to 2005... and if there is an overloaded method of GetFiles in .NET 2.0 then i'm really very happy..

    thanks

    thanks

    thank u very much

  4. #4

    Thread Starter
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Re: URGENT : How to find total files + folder

    Ok..It's working fine .. but i want to count only those file not having the Attribute Hidden or System... Any Idea ?

  5. #5
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476

    Re: URGENT : How to find total files + folder

    You could do something like this:

    Code:
    if(File.GetAttributes(path) != System.IO.FileAttributes.Hidden || File.GetAttributes(path) != System.IO.FileAttributes.System)
    Stephan
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

  6. #6

    Thread Starter
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Re: URGENT : How to find total files + folder

    Ok .. But how to use it with the coding like this :

    VB Code:
    1. Int32 Totalcount = System.IO.Directory.GetDirectories("c:/", "*", System.IO.SearchOption.AllDirectories).Length;

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

    Re: URGENT : How to find total files + folder

    You cannot do direct comparisons of file attributes like that because it is a compound value. You'd need to do something like this:
    Code:
    string[] files = System.IO.Directory.GetFiles("folder path here");
    int fileCount = files.Length;
    
    foreach (string file in files)
    {
        System.IO.FileAttributes attr = System.IO.File.GetAttributes(file);
    
        // These two conditions could be combined but I've separated them for clarity.
        // You must perform a bit-wise AND to extract individual values from the composite value.
        if (attr & System.IO.FileAttributes.System == System.IO.FileAttributes.System)
        {
            fileCount--;
        }
        else if (attr & System.IO.FileAttributes.Hidden == System.IO.FileAttributes.Hidden)
        {
            fileCount--;
        }
    }
    
    MessageBox.Show(fileCount + " files found.");
    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

  8. #8

    Thread Starter
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Re: URGENT : How to find total files + folder

    Finally I installed .NET 2005 .. and hey the above solution is working fine with minor changes.. thanks dude

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