{"id":37,"date":"2011-12-14T12:02:59","date_gmt":"2011-12-14T18:02:59","guid":{"rendered":"https:\/\/www.asberry.org\/blog_tech\/?p=37"},"modified":"2011-12-15T07:22:06","modified_gmt":"2011-12-15T13:22:06","slug":"c-to-delete-files-older-than-x-days-from-a-directory","status":"publish","type":"post","link":"https:\/\/asberry.org\/blog_tech\/?p=37","title":{"rendered":"C# to delete files older than X days from a directory"},"content":{"rendered":"<p>The vast majority of the time, when I build an integration I archive all the files so that they can be used to track down where something is going wrong.\u00a0 Unfortunately if left unchecked these files can grow and grow and it becomes impossible (or rather terribly difficult) to sort through the files to see if there was indeed an issue.\u00a0 The answer to this is to cull down the files when you archive them.\u00a0 To this end it&#8217;s good to kill all files older than X days.\u00a0 This is a console application that can do this, it actually does this in 3 directories.\u00a0 Path, path1 and path2 define the directories and days is the amount of days back to delete.<\/p>\n<pre class=\"brush: csharp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.IO;\r\n\r\nnamespace TestConsoleApplication\r\n{\r\nclass Program\r\n{\r\nstatic void Main(string&#x5B;] args)\r\n{\r\nstring path, path2, path3;\r\nint days;\r\nSystem.DateTime date;\r\nSystem.DateTime keepdate;\r\npath = &quot;Y:\\\\SQL_Backups\\\\SFDC_Data&quot;;\r\npath2 = &quot;Y:\\\\SQL_Backups\\\\TaleoData&quot;;\r\npath3 = &quot;Y:\\\\SQL_Backups\\\\dnn5_test&quot;;\r\ndays = 10;\r\ndate = (DateTime.Now);\r\nkeepdate = date.AddDays(-days);\r\n\r\nDirectoryInfo fileListing = new DirectoryInfo(@path);\r\n\r\nforeach (FileInfo f in fileListing.GetFiles())\r\n\r\n{\r\nif (f.LastWriteTime &lt;= keepdate)\r\nf.Delete();\r\n}\r\n\r\nDirectoryInfo fileListing2 = new DirectoryInfo(@path2);\r\n\r\nforeach (FileInfo f in fileListing2.GetFiles())\r\n{\r\nif (f.LastWriteTime &lt;= keepdate)\r\nf.Delete();\r\n}\r\n\r\nDirectoryInfo fileListing3 = new DirectoryInfo(@path3);\r\n\r\nforeach (FileInfo f in fileListing3.GetFiles())\r\n{\r\nif (f.LastWriteTime &lt;= keepdate)\r\nf.Delete();\r\n}\r\n\r\n}\r\n}\r\n}\r\n<\/pre>\n<p>I have yet to get this functionality to work in SSIS but will post how to do that once I figure it out.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The vast majority of the time, when I build an integration I archive all the files so that they can be used to track down where something is going wrong.\u00a0 Unfortunately if left unchecked these files can grow and grow and it becomes impossible (or rather terribly difficult) to sort through the files to see [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-c","author-aron"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4bBkH-B","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=37"}],"version-history":[{"count":5,"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":49,"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions\/49"}],"wp:attachment":[{"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/asberry.org\/blog_tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}