<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://beyondrelational.com/live/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tag 'retention'</title><link>http://beyondrelational.com/live/search/SearchResults.aspx?a=1&amp;o=DateDescending&amp;tag=retention&amp;orTags=0</link><description>Search results matching tag 'retention'</description><dc:language>en-US</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>SSIS - Delete files older than retention period from folder with parameter using Script Task in SQL Server</title><link>http://beyondrelational.com/live/blogs/paresh/archive/2012/02/10/ssis-delete-files-older-than-retention-period-from-folder-with-parameter-using-script-task-in-sql-server.aspx</link><pubDate>Fri, 10 Feb 2012 14:03:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:15382</guid><dc:creator>pprajapati</dc:creator><description>&lt;div dir="ltr" style="text-align:left;"&gt;
After using some of the ways to delete the files from the&amp;nbsp;particular&amp;nbsp;folder like&amp;nbsp;&lt;a href="http://beyondrelational.com/blogs/paresh/archive/2011/12/30/ssis-delete-files-from-specified-folder-using-script-task-in-sql-server.aspx" target="_blank"&gt;&lt;strong&gt;Delete file using Script Task&lt;/strong&gt;&lt;/a&gt; and &amp;nbsp;&lt;a href="http://beyondrelational.com/blogs/paresh/archive/2011/12/23/ssis-delete-files-from-specified-folder-using-file-system-task-in-sql-server.aspx" target="_blank"&gt;&lt;strong&gt;File System Task&lt;/strong&gt;&lt;/a&gt; in SSIS. We have seen such methods and used&amp;nbsp;logic to get the files from the loop container and process to delete them and same thing applied for the scripts as well.&amp;nbsp;we also looked for the files deletion which are older than some&lt;strong&gt;&lt;span style="text-decoration:underline;"&gt;&amp;nbsp;&lt;a href="http://beyondrelational.com/blogs/paresh/archive/2012/02/04/ssis-delete-files-older-than-retention-period-from-specified-folder-using-script-task-in-sql-server.aspx" target="_blank"&gt;Retention&amp;nbsp;period with Script Task&lt;/a&gt;&lt;/span&gt;&lt;/strong&gt; also.&lt;br /&gt;
&lt;br /&gt;
In earlier posts we have not used any parameters in the scripts and directly applied folder path and retention period values there. But here i would like to use parameters for the folder path and&amp;nbsp;retention&amp;nbsp;period and pass through the scripts and delete them as per condition.&lt;br /&gt;
&lt;br /&gt;
1. Let check the files from the target folder.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-U4Ss7HEZyvU/TvM69jF50NI/AAAAAAAABMA/U40LUTPZo9g/s1600/deletefile4_0.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="312" src="http://1.bp.blogspot.com/-U4Ss7HEZyvU/TvM69jF50NI/AAAAAAAABMA/U40LUTPZo9g/s640/deletefile4_0.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
2. Create parameters and set the values. Here it is going to be delete the files which are older then 3 days from the&amp;nbsp;E:\ImagesBackup folder in this case.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-FcJ1ygMy1mE/TvM7sJR_jII/AAAAAAAABM0/3ErvmvY1XrE/s1600/deletefile4_1.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="140" src="http://2.bp.blogspot.com/-FcJ1ygMy1mE/TvM7sJR_jII/AAAAAAAABM0/3ErvmvY1XrE/s640/deletefile4_1.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;
3. Drag and drop File System Task&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-CyTAkEEkW1M/TvM76LwEgnI/AAAAAAAABNA/ajEwKxBt9Nk/s1600/deletefile4_2.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="270" src="http://3.bp.blogspot.com/-CyTAkEEkW1M/TvM76LwEgnI/AAAAAAAABNA/ajEwKxBt9Nk/s640/deletefile4_2.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;
4. Put the parameters as ReadOnlyVariables&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-YVP1uU7witM/TvM8bOuAVCI/AAAAAAAABNM/Vj-O9qZ8lXk/s1600/deletefile4_3.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="542" src="http://3.bp.blogspot.com/-YVP1uU7witM/TvM8bOuAVCI/AAAAAAAABNM/Vj-O9qZ8lXk/s640/deletefile4_3.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;
5. Apply attached script in editor which have additional logic with condition to check the file lat&amp;nbsp;modified&amp;nbsp;date and check if older than specified retention period or not. Here you can see the parameters used in the script.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-AnTo_WNfcOQ/TvM8rjPMM5I/AAAAAAAABNY/0RjlglyP5Xw/s1600/deletefile4_4.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="368" src="http://1.bp.blogspot.com/-AnTo_WNfcOQ/TvM8rjPMM5I/AAAAAAAABNY/0RjlglyP5Xw/s640/deletefile4_4.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;span style="color:#0b5394;"&gt;Please note here we need to import system.IO namespace.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
6. Turn on final step and run package. Files older than specified retention period&amp;nbsp;get deleted.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-sc_dCAhT6mY/TvM8xEHV6LI/AAAAAAAABNk/bk-yUwdhK8k/s1600/deletefile4_5.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="430" src="http://2.bp.blogspot.com/-sc_dCAhT6mY/TvM8xEHV6LI/AAAAAAAABNk/bk-yUwdhK8k/s640/deletefile4_5.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
You &amp;nbsp;can use the script below for the same as mentioned in above image,&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:sql"&gt;--// You need to apply below one line in &amp;quot;namespaces&amp;quot; region.
using System.IO;

--//You need to apply below lines inplace of &amp;nbsp;// TODO: Add your code here
int RetentionPeriod = Convert.ToInt32(Dts.Variables[&amp;quot;User::Period&amp;quot;].Value.ToString());
string directoryPath = Dts.Variables[&amp;quot;User::BackupFolder&amp;quot;].Value.ToString();
string[] oldFiles = System.IO.Directory.GetFiles(directoryPath, &amp;quot;*.*&amp;quot;);

foreach (string currFile in oldFiles)
  {
         FileInfo currFileInfo = new FileInfo(currFile);
              
            if (currFileInfo.LastWriteTime &amp;lt; (DateTime.Now.AddDays(-RetentionPeriod)))
                {
                    currFileInfo.Delete();
                }
            }
&lt;/pre&gt;
Hope you liked this post. Stay tuned for more.  &lt;/div&gt;</description></item><item><title>SSIS - Delete files older than retention period from specified folder using Script Task in SQL Server</title><link>http://beyondrelational.com/live/blogs/paresh/archive/2012/02/04/ssis-delete-files-older-than-retention-period-from-specified-folder-using-script-task-in-sql-server.aspx</link><pubDate>Sat, 04 Feb 2012 13:18:00 GMT</pubDate><guid isPermaLink="false">6e5011fa-7db5-4df3-bb79-9085c1d333b3:15316</guid><dc:creator>pprajapati</dc:creator><description>&lt;div dir="ltr" style="text-align:left;"&gt;
Earlier we have seen for the &lt;a href="http://beyondrelational.com/blogs/paresh/archive/2011/12/30/ssis-delete-files-from-specified-folder-using-script-task-in-sql-server.aspx" target="_blank"&gt;&lt;span style="font-size:large;"&gt;Delete file using Script Task&lt;/span&gt;&lt;/a&gt; and&amp;nbsp;&lt;a href="http://beyondrelational.com/blogs/paresh/archive/2011/12/23/ssis-delete-files-from-specified-folder-using-file-system-task-in-sql-server.aspx" target="_blank"&gt;&lt;span style="font-size:large;"&gt;files deletion using File System Task&lt;/span&gt;&lt;/a&gt; in SSIS. We have directly wrote the logic to get the files and delete from specified folder in the script for the Script Task. For File System Task we have Used Foreach Loop Container and variables to hold file names which are passed from earlier stage one by one and then finally used with File System Task to delete it.&lt;br /&gt;
&lt;br /&gt;
Here we have same but not to delete all the files inside passed folder but get deleted files which are older than specified days. This is nothing but minor change in script where we pass the folder path and retention period. Lets start the steps as following,&lt;br /&gt;
&lt;br /&gt;
1. Let check the files from the target folder.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-FlO0C9E5IFU/TushRopSsMI/AAAAAAAABKs/T-AdTCQUUdA/s1600/deletefile3_0.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="272" src="http://2.bp.blogspot.com/-FlO0C9E5IFU/TushRopSsMI/AAAAAAAABKs/T-AdTCQUUdA/s640/deletefile3_0.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
2. Drag and drop Script Task.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-gOKw4ZrAsfQ/TushcJrTheI/AAAAAAAABK0/shWADAXV0FM/s1600/deletefile3_1.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="346" src="http://1.bp.blogspot.com/-gOKw4ZrAsfQ/TushcJrTheI/AAAAAAAABK0/shWADAXV0FM/s640/deletefile3_1.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
3. Open script editor from the properties.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-SP5FhW40Ls8/TushiqK2JbI/AAAAAAAABK8/pOUQM8wHuSE/s1600/deletefile3_2.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="544" src="http://2.bp.blogspot.com/-SP5FhW40Ls8/TushiqK2JbI/AAAAAAAABK8/pOUQM8wHuSE/s640/deletefile3_2.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
4. Apply attached script in editor which have additional logic with condition to check the file lat&amp;nbsp;modified&amp;nbsp;date and check if older than specified retention period or not.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-tCKDdklF7Po/TusiAyYl62I/AAAAAAAABLE/9G62pdnrnG0/s1600/deletefile3_3.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="378" src="http://4.bp.blogspot.com/-tCKDdklF7Po/TusiAyYl62I/AAAAAAAABLE/9G62pdnrnG0/s640/deletefile3_3.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;span style="color:#0b5394;"&gt;Please note here we need to import system.IO namespace.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
5. Turn on final step and run package. Files older than specified retention period&amp;nbsp;get deleted.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear:both;text-align:center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-8QOxRoVrpAw/TusiMIb3b3I/AAAAAAAABLM/RXWA8Oe3Vm8/s1600/deletefile3_4.png" style="clear:left;float:left;margin-bottom:1em;margin-right:1em;"&gt;&lt;img border="0" height="472" src="http://4.bp.blogspot.com/-8QOxRoVrpAw/TusiMIb3b3I/AAAAAAAABLM/RXWA8Oe3Vm8/s640/deletefile3_4.png" width="640" alt="" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;p&gt;
You can also find script code here,&lt;/p&gt;
&lt;pre class="brush:sql"&gt;--// You need to apply below one line in &amp;quot;namespaces&amp;quot; region.
using System.IO;

--//You need to apply below lines inplace of &amp;nbsp;// TODO: Add your code here
int RetentionPeriod = 3;
string directoryPath = @&amp;quot;E:\ImagesBackup&amp;quot;;
string[] oldFiles = System.IO.Directory.GetFiles(directoryPath, &amp;quot;*.png&amp;quot;);
     foreach (string currFile in oldFiles)
            {
                FileInfo currFileInfo = new FileInfo(currFile);
                if (currFileInfo.LastWriteTime &amp;lt; (DateTime.Now.AddDays(-RetentionPeriod)))
                {
                    currFileInfo.Delete();
                }
            }
&lt;/pre&gt;
&lt;p&gt;
&lt;br /&gt;
&lt;br /&gt;
Hope you liked this post. Stay tuned for more.&lt;/p&gt;</description></item></channel></rss>