Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
Hi,

I would like a program that surveys a NTFS file system in one drive, for example, drive D in your computer. The survey results must show and count all of files and programs in 3 groups.
group1 : size of files 0 - 10 kilo bytes.
group2 : size of files 10 - 1000 kilo bytes.
group3 : size of files 1000 - 100000 kilo bytes.

The main focus of this project is counting file size of Drive(example) in 3 three groups, that above-mentioned.
Posted
Updated 17-Nov-11 1:07am
v3
Comments
johannesnestler 19-Nov-11 5:20am    
so mamadss - homework done? please accept my solution if you like it.

What seems to be the problem, nobody is stopping you writing the program/code you want.

Use Google to get ideas and start coding.

Once you have code and run into problems you can always come back here with a more specific question and the community will do its best to help you. Don't forget to post only the relevant code bits that pose the problem as a code dump is not usually helpful at all in getting someone to help/assist you.

Good luck and happy coding.
 
Share this answer
 
Comments
Pandya Anil 17-Nov-11 7:52am    
this is not answer, you could have added it as a comment.. :), who rated it 5 ?
Richard MacCutchan 17-Nov-11 13:58pm    
Probably someone who appreciates that it is a valid answer to a rather silly question.
Recursion is your solution :

C#
class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo tDir = new DirectoryInfo(@"C:\");
            string Pattern = "a";
             TraverseDirs(tDir, Pattern);
            Console.Read();
        }
         private static void TraverseDirs(DirectoryInfo dir, string Pattern)
         {            // Subdirs
            try         // Avoid errors such as "Access Denied"
            {
                foreach (DirectoryInfo iInfo in dir.GetDirectories())
                {
                    if (iInfo.Name.StartsWith(Pattern))
                        Console.WriteLine("Found dir:  " + iInfo.FullName);
                     TraverseDirs(iInfo, Pattern);
                }
            }
            catch (Exception)
            {
            }
             // Subfiles
            try         // Avoid errors such as "Access Denied"
            {
                foreach (FileInfo iInfo in dir.GetFiles())
                {
                    if (iInfo.Name.StartsWith(Pattern))
                        Console.WriteLine("Found file: " + iInfo.FullName);
                }
            }
            catch (Exception)
            {
            }
        }    
}


This is not exactly what you want, but I hope you can figure out based on this, some reference is also provided below.

http://www.dotnetperls.com/recursively-find-files[^]

http://msdn.microsoft.com/en-us/library/bb513869.aspx[^]

http://rosettacode.org/wiki/Walk_a_directory/Recursively#C.23[^]
 
Share this answer
 
v2
You asked for code? Here is it: (just copy to a console project and replace Program.cs with the following code)
(Keep in mind that file sizes on drives are not meassured like 1kB = 1000B but 1kB = 1024B, ...)
Counting can take some time on big drives with a lot of files!

C#
using System;
using System.IO;

namespace CountFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            string strRootPath = @"D:\";
            int iCount10kB = 0,iCount1000kB = 0,iCount100000kB = 0, iCountBigger = 0;
            CountFiles(strRootPath,
                ref iCount10kB, ref iCount1000kB, ref iCount100000kB, ref iCountBigger);
            Console.WriteLine("Results for root directory {0}", strRootPath);
            Console.WriteLine("Files 0kB - 10 kB:           {0}", iCount10kB);
            Console.WriteLine("Files 10kB - 1000 kB:        {0}", iCount1000kB);
            Console.WriteLine("Files 1000kB - 100000 kB:    {0}", iCount100000kB);
            Console.WriteLine("Files bigger than 100000 kB: {0}", iCountBigger);
            Console.ReadKey();
        }

        static void CountFiles(string strRootPath,
            ref int iCount10kB, ref int iCount1000kB, ref int iCount100000kB, ref int iCountBigger)
        {
            try
            {
                string[] astrFiles = Directory.GetFiles(strRootPath);
                foreach (string strFile in astrFiles)
                {
                    long iFileLength = new FileInfo(strFile).Length;
                    if (iFileLength < 10000)
                        iCount10kB++;
                    else if (iFileLength < 1000000)
                        iCount1000kB++;
                    else if (iFileLength < 100000000)
                        iCount100000kB++;
                    else
                        iCountBigger++;
                }
            }
            catch
            {
                // ignore file access errors
            }

            try
            {
                string[] astrSubDirs = Directory.GetDirectories(strRootPath);
                foreach (string strSubDir in astrSubDirs)
                {
                    CountFiles(strSubDir, // recursion
                        ref iCount10kB, ref iCount1000kB, ref iCount100000kB, ref iCountBigger);
                }
            }
            catch
            {
                // ignore directory access errors
            }
        }
    }
}
 
Share this answer
 
v2
Comments
johannesnestler 19-Nov-11 5:19am    
who down voted? this code is tested and solves the OPs homework. I don't like the "gimme code" people, but I had 5 min. time and it's a funny example for recursion.
mamadss 19-Nov-11 6:17am    
where is program.cs??
johannesnestler 19-Nov-11 17:02pm    
if you have visual studio express start a new console project. there will be a file called program.cs - replace its content with the above code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900