473,806 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delete old files

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
3 3390
On Nov 21, 9:33 am, "Matt" <M...@community .nospam.comwrot e:
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
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 lastmodifieddat e
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
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*******@yaho o.NoSpamMaam.co mwrote in message
news:F6******** *************** ***********@mic rosoft.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
lastmodifieddat e
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
15673
by: Ben | last post by:
Greetings, I am looking for a way to search for and delete files based on a pattern mask. For example, the search method would find all files matching a certain pattern containing wildcards (e.g. *FILE29*.TXT). I'm looking for a way to do this in either Visual Basic or C(++). Thanks!
5
14155
by: Jobs | last post by:
Hello All, I want to delete all files in a directory. I am making a backup copy of all files in the directories say c:\abc by reading and writing to a file. After making a backup copy I want to delete these files. I am using following code: // get list of all files in the directories
2
28530
by: U C | last post by:
Hi, Can i delete files in cdump folder. Whats the basic use of having files in cdump folder. Is there any need to take backup when deleting the same. I am having solaris if we can delet then what is the command for the same. thanks Regards umesh
5
13279
by: Raj | last post by:
Hi all, Can anyone help me with a script which would delete files or move them to a different folder at some scheduled time..! Please.....!!! Thanks in advance...
6
11610
by: Sonoman | last post by:
Hi all: I would like to find out how to write a small program that deletes files. I want to be able to delete all files from a known directory (i.e. cookies, temp files, etc.) regardless of type and name, "or" may be by extension. This way I could have it run on start up and I will have my computer free of junk every time I reboot. What should I use? Please advise.
3
20086
by: Krazitchek | last post by:
Hi, how do i do to delete files with a specific extension (like *.lnk). I try File.Delete(@"e:\test\\*.lnk) but it does not work, not the good way i guess... Help please, thanks.
3
2620
by: Scott_Tuttle | last post by:
I'm trying to delete files with strange characters in the names but csharp doesnt seem to be able to see them at all. Solution??
1
2346
by: Keith Henderson | last post by:
It seems that I will soon have to write a windows service (or if there's something more appripriate?) that will delete files off of a hard drive. Here's an example, a file will be placed in a directory, at the time it is placed there, a row will be inserted into an RDBMS table which gives the filename, the location (could be on various servers), its time-to-live (like 2 days) and the datetime it was placed there, and probably a flag...
4
5952
by: rob | last post by:
Does C# 2005 have an easy way to delete files to the recycle bin?
1
1816
by: amituts | last post by:
Hi! I need a script which delete files in a directory when there are more than 8 files exist in that directory. It should delete oldest file and 3 latest file should be remains in that directory. file format : read_story_12_03_2010.zip read_story_13_03_2010.zip Thanks in advance for ur help.
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10618
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10110
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3850
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.