One challenge I have faced recently is to automatically delete files which are over X days old. Than 30 then the expression. Deleting Date Stamped Files Over X. It's actually three commands run through a single pipeline: 1. Get the dir listing and pass results into the pipeline 2. Query the pipeline for files with a LastWriteTime older than 14 days and pass the results through the pipeline 3. Delete all the files in the pipeline Credit to The PowerShell Guy for the boilerplate code.
Active4 months ago
I would like to know (using C#) how I can delete files in a certain directory older than 3 months, but I guess the date period could be flexible.
Just to be clear: I am looking for files that are older than 90 days, in other words files created less than 90 days ago should be kept, all others deleted.
bluish
15.1k1919 gold badges9595 silver badges154154 bronze badges
JL.JL.
33.1k110110 gold badges287287 silver badges430430 bronze badges
16 AnswersSteve DannerSteve Danner
19k77 gold badges3030 silver badges4747 bronze badges
Uri AbramsonUri Abramson
4,01755 gold badges2929 silver badges5252 bronze badges
Samuel NeffSamuel Neff
57.6k1414 gold badges110110 silver badges157157 bronze badges
Here's a snippet of how to get the creation time of files in the directory and find those which have been created 3 months ago (90 days ago to be exact):
Pierre-Luc ChampignyPierre-Luc Champigny
The GetLastAccessTime property on the System.IO.File class should help.
Keith BloomKeith Bloom
1,86622 gold badges1515 silver badges2727 bronze badges
Basically you can use Directory.Getfiles(Path) to get a list of all the files. After that you loop through the list and call GetLastAccessTim() as Keith suggested.
Ian JacobsIan Jacobs
5,14411 gold badge1818 silver badges3333 bronze badges
you just need FileInfo -> CreationTime
and than just calculate the time difference.
in the app.config you can save the TimeSpan value of how old the file must be to be deleted
also check out the DateTime Subtract method.
good luck
nWorxnWorx
Ssis Delete Files Older Than 30 Days
Alternatively, you can use the File.GetCreationTime Method if you need to delete files based on creation dates.
jinsungyjinsungy
![]()
6,3062424 gold badges6464 silver badges7676 bronze badges
Yiannis LeoussisYiannis Leoussis
Ssis Delete File
use in page load or webservice or any other use.
My concept is evrry 7 day i have to delete folder file without using DB
Ssis Delete Files Older Than
vishalvishal
Arihant LodhaArihant Lodha
Autocad 2007 software free download. https://heavyswag973.weebly.com/sony-vegas-pro-11-serial-number-authentication-code.html. i have try this code and it works very well, hope this answered
Rosidin BimaRosidin Bima
Rachit RastogiRachit Rastogi
For example:To go My folder project on source, i need to up two folder.I make this algorim to 2 days week and into four hour
Maximiliano CesánMaximiliano Cesán
Ssis Delete Old Files
I use the following in a console app, running as a service, to get directory info from the App.Settings file. Number of days to keep the files is also configurable, multiplied by -1 for use in the AddDays() method of DateTime.Now.
n122vun122vu
Allan FAllan F
Delete Files Older Than 30 DaysNot the answer you're looking for? Browse other questions tagged c#.netfiledirectory or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |