Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I can count files in a shared folder with C# but we are needing to know the amount for a month at a time like the moth of October. There is to many to manually count manually.

Here is the code I have to count all files in the folder location:

C#
<pre lang="cs">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileCount
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"\\\\server\path");
            int count = dir.GetFiles().Length;
            string scount = count.ToString();
            label1.Text = scount;
Posted
Comments
ZurdoDev 21-Nov-13 16:57pm    
How do you know what files to count for which month?
Member 10264652 21-Nov-13 17:00pm    
All files are of the same type and I was thinking of searching by created date since they are created and processed 95% of time same day and moved to the folder that we want to count.

1 solution

I'd use a method like:
C#
// required
using System.Collections.Generic;
using System.IO;
using System.Linq;

private List<FileInfo> GetFilesByMonth(int monthToGet, string directoryPath)
{
    DirectoryInfo dir = new DirectoryInfo(directoryPath);

    //  note use of the option to search sub-directories
    FileInfo[] theFiles = dir.GetFiles("*", SearchOption.AllDirectories);

    return theFiles.Where(fl => fl.CreationTime.Month == monthToGet).ToList();
}
Which can be called like this:
C#
List<FileInfo> FilesInNovember = GetFilesByMonth(11, @"\\\\server\path");
 
Share this answer
 
Comments
walterhevedeich 21-Nov-13 20:20pm    
+5. I was preparing an old school answer (counting the files in a loop) and then I saw your answer, which makes more sense. :)
Member 10264652 22-Nov-13 10:07am    
EDIT:
I was missing the return line. now I don't need to search subdirectories, all the files are in one location for 90 days then deleted.

First, Thank you for your help! I am trying this but the line:
private List<fileinfo> GetFilesByMonth(int monthToGet, string directoryPath)

The GetFilesByMonth has an error stating that not all code paths return a value.
Member 10264652 22-Nov-13 10:11am    
got it without errors and removed option to search subdirectories. Doesn't pay to look at code before having coffee.
Member 10264652 22-Nov-13 11:09am    
ok, I am still having problems, I am wanting to take the amount of files for the month and set a label to the number but I get nothing. numfiles is set as string
List<fileinfo> FilesInNovember = GetFilesByMonth(11, @"\\\\server\path");
numfiles = FilesInNovember.ToString();

label2.Text = numfiles;
Member 10264652 22-Nov-13 15:14pm    
I figured it out and got it to work, just had to step back from it and come back to it. I get tunnel vision and forget to step back. Thanks!!!! Working code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileCount
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

//System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"\\\\nspeia\c$\einterface\Inbound\completed");
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"\\\\nspeia\c$\einterface\Inbound\completed");
int count = dir.GetFiles().Length;
string scount = count.ToString();
label1.Text = scount;



int numfiles;
List<fileinfo> FilesInNovember = GetFilesByMonth(11, @"\\\\nspeia\c$\einterface\Inbound\completed");
numfiles = FilesInNovember.Count();
label2.Text = numfiles.ToString();


}
private List<fileinfo> GetFilesByMonth(int monthToGet, string directoryPath)
{



DirectoryInfo dir = new DirectoryInfo(directoryPath);
FileInfo[] theFiles = dir.GetFiles("*");
return theFiles.Where(fl => fl.CreationTime.Month == monthToGet).ToList();
}
}
}

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