Automatic Delete files

mathanraj76

Member
Joined
Feb 4, 2013
Messages
6
Programming Experience
Beginner
Hi..

I want to know how to delete files from folder automaticaly

if the files creation date more then 7 days it delete the files automatically from the folder

I have done but in manual way..

If have pls guide me and show me some sample codes

thnks
raj
 
Hi..

I want to know how to delete files from folder automaticaly

if the files creation date more then 7 days it delete the files automatically from the folder

I have done but in manual way..

If have pls guide me and show me some sample codes

thnks
raj
Presumably what you actually mean is that you want to delete the files in code rather than in Windows Explorer. There are a few variations on a theme but they all perform basically the same steps:

1. Get a list of files from the folder.
2. Check each one for its creation time.
3. Delete each one that satisfies your criterion.

As for specifics, here's what I would recommend. First, create a DirectoryInfo for your folder. Next, call its GetFiles method to get an array of FileInfo objects. You can then loop through that array and check each one. To create your threshold date/time, you use Date.Now or Date.Today and its AddDays method. The FileInfos will have a property for their creation time and you can compare that to your threshold date/time. For any that are older, they have a method for deleting the file.

See what you can come up with from that and post back if you have issues. Make sure that you don't try to do it all in one go. There's no point even thinking about deleting files if you can get the list of files in the first place. Prove that you can get a correctly populated FileInfo array before considering anything else. Etc.
 
Back
Top