<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>youdidwhatwithtsql.com &#187; archiving files</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/archiving-files/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Wed, 01 Sep 2010 17:02:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>SSIS: Archiving files with VB.Net</title>
		<link>http://www.youdidwhatwithtsql.com/ssis-archiving-files-with-vb-net/402</link>
		<comments>http://www.youdidwhatwithtsql.com/ssis-archiving-files-with-vb-net/402#comments</comments>
		<pubDate>Thu, 15 Oct 2009 21:06:37 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[SSIS]]></category>
		<category><![CDATA[archiving files]]></category>
		<category><![CDATA[VB.Net]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/ssis-archiving-files-with-vb-net/402</guid>
		<description><![CDATA[When creating SSIS packages it’s a common requirement to be able to archive the processed files. Here’s VB.Net code snippet, with a quick walkthrough, that does exactly that. The script uses two variables, inFiles and archiveFiles. The variable inFiles contains the folder we want to move files from, and archiveFiles is the destination. Configure your [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-archiving-files-with-vb-net/402">SSIS: Archiving files with VB.Net</a></p>
]]></description>
			<content:encoded><![CDATA[<p>When creating <a href="http://www.microsoft.com/sqlserver/2005/en/us/integration-services.aspx" target="_blank">SSIS</a> packages it’s a common requirement to be able to archive the processed files. Here’s <a href="http://en.wikipedia.org/wiki/Visual_Basic_.NET" target="_blank">VB.Net</a> code snippet, with a quick walkthrough, that does exactly that. </p>
<p>The script uses two variables, <strong>inFiles </strong>and <strong>archiveFiles</strong>. The variable <strong>inFiles</strong> contains the folder we want to move files from, and <strong>archiveFiles</strong> is the destination.</p>
<p>Configure your variables in <a href="http://msdn.microsoft.com/en-us/library/ms173767.aspx" target="_blank">BIDS</a> like below.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/ssis_variables.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="ssis variables for archiving files" border="0" alt="ssis variables for archiving files" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/ssis_variables_thumb.png" width="644" height="133" /></a> </p>
<p>Add a <a href="http://msdn.microsoft.com/en-us/library/ms141752.aspx" target="_blank">script task</a> to the designer.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/archive_files_script_task.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Script task for archiving files in BIDS" border="0" alt="Script task for archiving files in BIDS" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/archive_files_script_task_thumb.png" width="244" height="124" /></a> </p>
<p>Right click the script task, choose ‘Edit’ then ‘Script’. Add the <strong>inFiles </strong>and <strong>archiveFiles</strong> variables to the<strong> ReadOnlyVariables </strong>textbox.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/script_task_config.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Script Task configuration for archiving files" border="0" alt="Script Task configuration for archiving files" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/script_task_config_thumb.png" width="570" height="484" /></a> </p>
<p>Click the “Design Script” button and paste the below code into the <a href="http://msdn.microsoft.com/en-us/library/ms974548.aspx" target="_blank">VSA</a> editor.</p>
<pre lang="VB.NET">
' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic
' The ScriptMain class is the entry point of the Script Task.

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

    ' The execution engine calls this method when the task executes.
    ' To access the object model, use the Dts object. Connections, variables, events,
    ' and logging features are available as static members of the Dts class.
    ' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
    '
    ' To open Code and Text Editor Help, press F1.
    ' To open Object Browser, press Ctrl+Alt+J.

    Public Sub Main()
        '
        ' Add your code here
        '
        Dim dir As System.IO.Directory
        ' fetch the folder locations from SSIS variables
        Dim inFiles As String = Dts.Variables("inFiles").Value.ToString
        Dim archiveFiles As String = Dts.Variables("archiveFiles").Value.ToString

        ' Get the system date and time separators for removing from the folder name we'll create later
        Dim dateSeparator As String = System.Globalization.DateTimeFormatInfo.CurrentInfo.DateSeparator
        Dim timeSeparator As String = System.Globalization.DateTimeFormatInfo.CurrentInfo.TimeSeparator

        ' Used to create a datetime stamped folder removing separators
        Dim dt As String = DateTime.Now.ToString.Replace(" ", "").Replace(dateSeparator, "").Replace(timeSeparator, "").Replace("/", "")

        ' Check folder paths end with a "\"
        If inFiles.EndsWith("\") = False Then
            inFiles &#038;= "\"
        End If

        If archiveFiles.EndsWith("\") = False Then
            archiveFiles &#038;= "\"
        End If

        archiveFiles &#038;= dt

        ' Create datetime stamp archive folder if it doesn't already exist
        If dir.Exists(archiveFiles) = False Then
            dir.CreateDirectory(archiveFiles)
        End If

        ' Move all files to archive
        For Each archFile As String In dir.GetFiles(inFiles)
            dir.Move(archFile, archiveFiles &#038; "\" &#038; archFile.Substring(archFile.LastIndexOf("\") + 1, archFile.Length - archFile.LastIndexOf("\") - 1))
        Next

        Dts.TaskResult = Dts.Results.Success

    End Sub

End Class
</pre>
<p>Place some files into the location your <strong>inFiles</strong> variable points to. Then right click the script task and choose “Execute Task”. </p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/archive_files_with_vb.net_script_task.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="archive_files_with_vb.net_script_task" border="0" alt="archive files with vb.net script task thumb SSIS: Archiving files with VB.Net" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/archive_files_with_vb.net_script_task_thumb.png" width="234" height="113" /></a> </p>
<p>Once the package has executed successfully the files will be moved to <strong>archiveFiles</strong> from <strong>inFiles</strong>.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/archive_folder_with_datetime.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="archive_folder_with_datetime" border="0" alt="archive folder with datetime thumb SSIS: Archiving files with VB.Net" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/10/archive_folder_with_datetime_thumb.png" width="244" height="146" /></a></p>
<p><map name='google_ad_map_402_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/402?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_402_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=402&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fssis-archiving-files-with-vb-net%2F402' title="SSIS: Archiving files with VB.Net" alt=" SSIS: Archiving files with VB.Net" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/ssis-archiving-files-with-vb-net/402">SSIS: Archiving files with VB.Net</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/ssis-archiving-files-with-vb-net/402/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
