How to check business days in c#
Asked Answered
N

5

6

My client gave me very diffrent requirement yesterday.

I have one folder at server where thousands of files are comming daily. He want me to write a logic to check dates of file. If files are there in folder from more than 3 business days (Mon to Friday) then he want me to delete those files

Example : If any file created in folder on Saturday then that file should delete on wednesday because inbetween we have Saturday and Sunday that should not count as business days.

My developemnt enviornment is c# .NET 3.5

I thought i should write custom method.

Please help me.

Nellienellir answered 24/11, 2011 at 7:50 Comment(2)
Do you mind holidays too? I mean, XMas, Easter and regional holidays?Hanafee
No, they want holiday as business day.Nellienellir
H
13

George Duckett solution is the one for you.
Just to help you I post an example:

public static class DateExtensions
{
    public static bool IsBusinessDay(this DateTime date)
    {
        return
            date.DayOfWeek != DayOfWeek.Saturday &&
            date.DayOfWeek != DayOfWeek.Sunday;
    }
    public static int BusinessDaysTo(this DateTime fromDate, DateTime toDate, 
                                     int maxAllowed = 0)
    {
        int ret = 0;
        DateTime dt = fromDate;
        while (dt < toDate)
        {
            if (dt.IsBusinessDay()) ret++;
            if (maxAllowed > 0 && ret == maxAllowed) return ret;
            dt = dt.AddDays(1);
        }
        return ret;
    }
}

With this you can do something like this:

DateTime from = DateTime.Now.AddDays(-8);
int ret = from.BusinessDaysTo(DateTime.Now);
int ret2 = from.BusinessDaysTo(DateTime.Now.AddDays(5), 8);
Hanafee answered 24/11, 2011 at 8:7 Comment(0)
D
4
  1. Enumerate all files using Directory.GetFiles.

    Directory.GetFiles(directoryPath)
    
  2. Get the creation date of the file using File.GetCreationTime.

    File.GetCreationTime(filePath)
    
  3. Get the business days between that date and today:

    Calculate the number of business days between two dates? and this answer.

  4. Write a LINQ query to work out whether the condition is satisfied.

    var FilesToDelete =
        from filePath in Directory.GetFiles("folder")
        where File.GetCreationTime(filePath).BusinessDaysUntil(DateTime.Today) > 3
        select filePath;
    
Dendrite answered 24/11, 2011 at 7:55 Comment(1)
I would use File.GetCreationTime instead of creating a new FileInfo for each file.Lights
A
1
    public static bool IsBusinessDay(this DateTime value)
    {
        if (value.DayOfWeek == DayOfWeek.Sunday) return false;
        if (value.DayOfWeek == DayOfWeek.Saturday) return false;

        return true;
    }

Remember to put extension methods in static classes.

Adulthood answered 24/11, 2011 at 7:56 Comment(1)
This doesn't answer the question.Summerlin
L
1

Just invoke:

var cleaner = new FileCleaner();
clenaer.DeleteOldFiles(@"C:\YourDiretory");

Code

public class FileCleaner
{
    public IEnumerable<string> GetOldFiles(string directoryName)
    {
        var limit = DateTime.Now.AddBusinessDays(-3);
        return Directory.GetFiles(directoryName)
                        .Where(file => File.GetCreationTime(file) < limit);
    }

    public void DeleteOldFiles(string directoryName)
    {
        foreach (var filename in GetOldFiles(directoryName))
        {
            File.Delete(filename);
        }
    }
}


public static class DateTimeExtensions
{
    public static bool IsBusinessDay(this DateTime instance)
    {
        return instance.DayOfWeek != DayOfWeek.Saturday 
               && instance.DayOfWeek != DayOfWeek.Sunday;
    }
    public static DateTime AddBusinessDays(this DateTime instance, int days)
    {
        var newDate = instance;
        while (days > 0)
        {
            newDate = newDate.AddDays(-1);
            if (newDate.IsBusinessDay())
                --days;
        }
        return newDate;
    }
}
Lights answered 24/11, 2011 at 8:25 Comment(0)
B
0

If you just need the file creation date, I wouldn't use the .NET classes, they create alot of overhead if there are thousands of files which need checking.

From my expirience with dealing a large amount of files in one directory(50k+) and performance is needed, I'd use FindFirstFile FindNextFile and FindClose for faster processing and less overhead.

WIN32_FIND_DATA findData;

DateTime.FromFileTimeUtc((long)findData.ftLastWriteTime.dwLowDateTime + (long)findData.ftLastWriteTime.dwHighDateTime * 4294967296

Broida answered 24/11, 2011 at 8:1 Comment(2)
In your experience, how much faster is the above code than using the .NET classes?Summerlin
@Summerlin I don't have the exact numbers since its been a while. It ranged from a magnitude of 1-5, depending on the amount of files.Broida

© 2022 - 2024 — McMap. All rights reserved.