By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,190 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,190 IT Pros & Developers. It's quick & easy.

delete old files

P: n/a
I have several folders on a server that contains files that go back 3-4 years. It was mentioned that keeping files that long is no longer needed. So my question is, is there a way to create a windows service in C# that can delete files in the folders that are older then 12 months?

The server is structured something like this:

DataLogs
Server1
App1 - this folder contains all the log files

or it can look something like this:
DataLogs
Server1
App1
Logs - the log files can be stored here
and another example:
DataLogs
App1 - logs stored here.
So is there a way to do a 'high level' search and delete all files that are older then 12 months or do I have to go through every folder and look? How would that be accomplished?

Nov 21 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Nov 21, 9:33 am, "Matt" <M...@community.nospam.comwrote:
I have several folders on a server that contains files that go back 3-4 years. It was mentioned that keeping files that long is no longer needed. So my question is, is there a way to create a windows service in C# that can delete files in the folders that are older then 12 months?
Yes.
So is there a way to do a 'high level' search and delete all files that are older then 12 months or do I have to go through every folder and look? How would that be accomplished?
You first need to figure out how to design a Windows Service
with .NET. There is plenty of help, in fact there is a Walkthrough
with step by step instructions on how to create a Windows Service
project in VS2005.

Once you have a service that starts up at boot time, and wakes up on
some predetermined interval and is ready to do some work, then you
need to give it some work to do. In you case, you want to do a
directory scan using the Directory Class to get a list of all files
and folders from a specified root. The loop through the files and
determine the creation date using the FileInfo class. Delete the
selected files with the File class.

Sounds like a fun project.
Nov 21 '07 #2

P: n/a
I think it would be overkill to have a Windows Service running 24hrs a day
taking up resources just to delete old files periodically. You would be
better served with a Console EXE that does the same thing and goes away when
it is done. You would simply have this run once a week (or whatever) under
Task Scheduler.

It should be easy to find recursive directory traversal code that will
delete all files (with whatever extension(s) ) that have a lastmodifieddate
of a certain value or older.

--Peter
"Inside every large program, there is a small program trying to get out."
http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://www.blogmetafinder.com

"Matt" wrote:
I have several folders on a server that contains files that go back 3-4 years. It was mentioned that keeping files that long is no longer needed. So my question is, is there a way to create a windows service in C# that can delete files in the folders that are older then 12 months?

The server is structured something like this:

DataLogs
Server1
App1 - this folder contains all the log files

or it can look something like this:
DataLogs
Server1
App1
Logs - the log files can be stored here
and another example:
DataLogs
App1 - logs stored here.
So is there a way to do a 'high level' search and delete all files that are older then 12 months or do I have to go through every folder and look? How would that be accomplished?
Nov 21 '07 #3

P: n/a
its going to be scheduled to run the 30th of every month so its not running
24 hours a day every day. I was thinking a service at first but the more I
thought about it, I'll create a console app with no user interaction
required and just have it scheduled.

"Peter Bromberg [C# MVP]" <pb*******@yahoo.NoSpamMaam.comwrote in message
news:F6**********************************@microsof t.com...
>I think it would be overkill to have a Windows Service running 24hrs a day
taking up resources just to delete old files periodically. You would be
better served with a Console EXE that does the same thing and goes away
when
it is done. You would simply have this run once a week (or whatever) under
Task Scheduler.

It should be easy to find recursive directory traversal code that will
delete all files (with whatever extension(s) ) that have a
lastmodifieddate
of a certain value or older.

--Peter
"Inside every large program, there is a small program trying to get out."
http://www.eggheadcafe.com
http://petesbloggerama.blogspot.com
http://www.blogmetafinder.com

"Matt" wrote:
>I have several folders on a server that contains files that go back 3-4
years. It was mentioned that keeping files that long is no longer needed.
So my question is, is there a way to create a windows service in C# that
can delete files in the folders that are older then 12 months?

The server is structured something like this:

DataLogs
Server1
App1 - this folder contains all the log files

or it can look something like this:
DataLogs
Server1
App1
Logs - the log files can be stored here
and another example:
DataLogs
App1 - logs stored here.
So is there a way to do a 'high level' search and delete all files that
are older then 12 months or do I have to go through every folder and
look? How would that be accomplished?

Nov 21 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.