3 Replies - 26415 Views - Last Post: 07 June 2009 - 04:15 PM Rate Topic: -----

#1 erosenko   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 10-May 09

how to delete files in a folder older than a set number of days

Posted 06 June 2009 - 11:35 AM

Hi all. I have another question on a different topic. I have a project I am working on, and it sounds pretty simple, and I have it almost done, but I am having a problem with one part.

Project is to take a small text file that has a path to a folder (i.e. c:\program files\project\my project) and a number of "keep days" and then look at all the files in that folder and delete out the ones that are older (LastWriteTime) than the "keep days". I have a seperate app a user can enter these two variables and write the text file. I have set this part up as a console app, b/c I am going to run it from Windows scheduler to automate the process.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace stcsFDLT
{
	class Program
	{
		static void Main(string[] args)
		{
			//declare variables
			string path;
			int days;
			string filename;
			System.DateTime date;
			System.DateTime keepdate;

			//this is the file for the folder to delete from and the 
			//number of days to keep
			filename = "C:\\Users\\Public\\Documents\\stcsfdlt.txt";

			//open the file
			System.IO.StreamReader objReader;
			objReader = new System.IO.StreamReader(filename);

			//read the text file for the path to remove from
			//and the number of keep days
			path = (objReader.ReadLine());
			days = int.Parse(objReader.ReadLine());

			//declare the current date/time
			date = (DateTime.Now);

			//this is just a check to make sure the path and keep days are correct
			Console.WriteLine(path);
			Console.WriteLine(days);

			//this is a check of the current business date
			Console.WriteLine(date);

			//calculate the current date - the number of keep days
			keepdate = date.AddDays(-days);

			//display the date minus the keep days
			Console.WriteLine(keepdate);


			//this is where it gets the directory and looks at
			//the files in the directory to compare the last write time
			//against the keepdate variable.

			DirectoryInfo fileListing = new DirectoryInfo(@path);

			foreach (FileInfo f in fileListing.GetFiles)

				{
					if (f.LastWriteTime <= keepdate)
						f.Delete();
				}
			


			//this just pauses the screen so you can see the output
			//in the console
			Console.WriteLine("Press any key too continue...");
			Console.ReadLine();


		}
	}
}



On my foreach in the line, "foreach (FileInfo f in fileListing.GetFiles)" I am getting an error: ForEach cannot operate on a 'method group'. Did you intend to invoke the 'method group'?

I am not even 100% sure I have the right code to get to the directory I defined in the path variable, look at the files in that directory, and remove the ones older than the days variable, but I should be close.

As always, help is greatly appreciated!

~Cheers

Is This A Good Question/Topic? 0
  • +

Replies To: how to delete files in a folder older than a set number of days

#2 T3hC13h   User is offline

  • D.I.C Regular

Reputation: 65
  • View blog
  • Posts: 337
  • Joined: 05-February 08

Re: how to delete files in a folder older than a set number of days

Posted 06 June 2009 - 12:02 PM

Your forgot the empty parentheses after GetFiles.
	foreach (FileInfo f in new DirectoryInfo(@path).GetFiles())

				{
					if (f.LastWriteTime <= keepdate)
						f.Delete();
				}


Was This Post Helpful? 0
  • +
  • -

#3 erosenko   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 10-May 09

Re: how to delete files in a folder older than a set number of days

Posted 06 June 2009 - 12:12 PM

Doh! Thanks. I thought it was something easy, just didn't catch it. Nothing like another set of eyes. I will try it out and let you know.
Was This Post Helpful? 0
  • +
  • -

#4 erosenko   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 10-May 09

Re: how to delete files in a folder older than a set number of days

Posted 07 June 2009 - 04:15 PM

View Posterosenko, on 6 Jun, 2009 - 11:12 AM, said:

Doh! Thanks. I thought it was something easy, just didn't catch it. Nothing like another set of eyes. I will try it out and let you know.



The () was it. Thanks for the assistance!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1