ASN
ASN

Reputation: 1863

Delete files which are older than today's date

I have some files in the format of "yyyyMMdd_hhmmss_abc.txt" in a particular location. using the below code i am able to get all the files with "_abc.txt" in that folder.

fileArray = Directory.GetFiles(@"C://Documents", "*abc.txt");
for (int i = 0; i < fileArray.Length; i++)
{
    fileArray[i] = Path.GetFileNameWithoutExtension(fileArray[i]);
    Console.WriteLine(fileArray[i]);
}

But now I'm thinking of reading the file name, split it and then convert into date time object so that i can check for the condition(older than today's date) and delete them.

for eg: 20160426_045823_abc.txt

I want to split it into 2016, 04 , 26 and then convert into date time object using Datetime d1 = new Datetime(2016,04,26) and then do other operations.

Is there any other way to solve this problem?

Thanks in advance

Upvotes: 1

Views: 1344

Answers (4)

Joel Mueller
Joel Mueller

Reputation: 28735

The filename is already in a sortable format based on the date. Instead of parsing bits of the filename into a DateTime object, why not create a filename based on today's date and filter your array of filenames down to only those that are string-comparison-less than your today's-date filename? It's probably quite a bit faster than doing date parsing on each filename, for large lists of files.

For example:

var path = "c:\\whatever";
var suffix = "_abc.txt";
var todaysFilename = DateTime.Now.ToString("yyyyMMdd_hhmmss") + suffix;
var filesToDelete = Directory.EnumerateFiles(path, "*" + suffix)
    .Select(Path.GetFileName)
    .Where(fileName => string.Compare(fileName, todaysFilename, StringComparison.Ordinal) < 0)
    .ToArray();

foreach (var file in filesToDelete)
{
    File.Delete(Path.Combine(path, file));
}

Upvotes: 2

sujith karivelil
sujith karivelil

Reputation: 29006

The following code can be used to get the collection of files having created date less than today's date, A simple iteration over the collection will help you to delete them as well: consider the code

Simple option:

foreach (var item in Directory.GetFiles(@"C://Documents", "*.txt")
                               .Where(x => new FileInfo(x).CreationTime.Date  < DateTime.Now.Date))
{
    File.Delete(item);
} 

Based on Filename:

var fileArray = Directory.GetFiles(@"C://Documents", "*abc.txt");
for (int i = 0; i < fileArray.Length; i++)
{
    DateTime fileNameTime;
    string fileName = Path.GetFileNameWithoutExtension(fileArray[i]).Replace("_abc", " ");
    fileNameTime = DateTime.ParseExact("yyyyMMdd_hhmmss", fileName, CultureInfo.InvariantCulture, DateTimeStyles.None);
    if (fileNameTime.Date < DateTime.Now.Date)
    {
        File.Delete(fileArray[i]);
    }
}

Please note : The best and effective option is the first one, what you need to do is assign the file-name as the dateTime at the time of creation of the file(if it is under your control) so that the things became easier for you

Upvotes: 2

Arina
Arina

Reputation: 91

You could take the name of the string and do a DateTime.ParseExact.

String dateString = Path.GetFileNameWithoutExtension(fileArray[i])
DateTime d5 = DateTime.ParseExact(dateString, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None)
if (d5.Date < DateTime.Now.Date)
{
    File.Delete(fileArray[i]);
}

This will take the first 8 characters of the string and parse do an exact parse on it. Also you will probably just want the filename without the path and extension.

https://msdn.microsoft.com/en-us/library/w2sa9yss%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Upvotes: 1

JSON
JSON

Reputation: 1182

You can get the DateTime a file was created like this

DateTime time = File.GetCreationTime(fileName);

Yes it's that easy.

Upvotes: 0

Related Questions