delete old files

M

Matt

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?
 
Z

zacks

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.
 
G

Guest

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
 
M

Matt

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top