C# .NET - Count number of files into specific location in folders tree

Asked By Aldo Liaks on 11-Sep-08 01:46 AM

Hi guys,

I need help with the following problem.

I have to develop an application that counts the number of files into a known folder (including all its subfolders).

Let's say I have disc "C" within C three different folders in into each folder I have files and more subfolders (like windows explorer).  I receive as a parameter the path of "C" and I have to return the number of files into "C" (including all subfolders).

I cannot use a recursive method because of memory issue.

Any help will be appreciated.

Thanks in advance,

Aldo.

solution

Perry replied to Aldo Liaks on 11-Sep-08 02:02 AM

Hi,

I have written one application where I needed this things to be done. I have pasted my code below:

allFiles = Directory.GetFiles(FolderPath);

count = allDoneFiles.Length => will give the number of files available in given folder

-Paresh

Get the files count

Binny ch replied to Aldo Liaks on 11-Sep-08 02:04 AM
 public int GetFileCount(string dir)
        {
          

            try
            {

                DirectoryInfo dirinfo = new DirectoryInfo(dir);
                DirectoryInfo[] subdirinfo = dirinfo.GetDirectories("*.*");
                FileInfo[] files = dirinfo.GetFiles();


                if (files.Length > 0)
                {

                    fileCount = files.Length + fileCount;

                }

                foreach (DirectoryInfo directory in subdirinfo)
                {
                    GetFileCount(directory.FullName.ToString());
                }
            }
            catch (System.Exception ex)
            {

            }
            label10.Text = "The number of files in the folder is    " + fileCount.ToString();
            return fileCount;

        
        }

try this....

Vasanthakumar D replied to Aldo Liaks on 11-Sep-08 02:14 AM

hi,

you cannot do this with out recursive function....


int count = 0;

private void GetFilesList(string strPath)

{

System.IO.DirectoryInfo xMainDir = new System.IO.DirectoryInfo(strPath);

System.IO.FileInfo[] filein = xMainDir.GetFiles();

count = filein.Length;

System.IO.DirectoryInfo[] dirin = xMainDir.GetDirectories();

for (int i = 0; i < dirin.Length; i++)

{

GetChildDetails(dirin[i].FullName);

}

}

private void GetChildDetails(string strPath)

{

System.IO.DirectoryInfo xMainDir = new System.IO.DirectoryInfo(strPath);

System.IO.FileInfo[] filein = xMainDir.GetFiles();

count += filein.Length;

System.IO.DirectoryInfo[] dirin = xMainDir.GetDirectories();

for (int i = 0; i < dirin.Length; i++)

{

GetChildDetails(dirin[i].FullName);

}

}

See this
Sagar P replied to Aldo Liaks on 11-Sep-08 02:22 AM

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("c:\\");
int count = dir.GetFiles().Length;


This will gives you the total file count of perticular directory.

If you want to find out all inside directory then you have to use recursive function .

Best Luck!!!!!!!!!!!!!!!!
Sujit.

SOLVED! - Thanks
Aldo Liaks replied to Sagar P on 14-Sep-08 07:27 AM
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("c:\\");
int count = dir.GetFiles().Length;