Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is one possible solution to achieve this with the help of <code>Foreach loop container</code>, <code>Script Task</code> and <code>File System Task</code>. You can do this without File System Task. However, I have used it to make use of the in-built control flow task to move the files. The example was created using <strong><code>SSIS 2005</code></strong>.</p> <p>The example assumes that the files will be named uniformly. So, the example uses the format <strong>File DD-MM-YYYY</strong>. For example, the files will be named <code>File 29-07-2011</code>, <code>File 15-08-2011</code> etc.</p> <p>On the SSIS package, create the following variables. In this example, the source files are stored in the folder location <code>F:\Temp\</code> and the files should be moved to the location *F:\Temp\Monthwise*. Within the destination folder, there will be folders for each month like July, August etc.</p> <ul> <li><p><strong>DestinationFolder</strong> variable will hold the final destination folder value like <code>F:\Temp\Monthwise\August</code> but this variable will be assigned with the actual value inside the Script task. For now, let's assign the value <code>F:\Temp\Monthwise\</code>. This temporary value is to avoid File System Task from throwing error messages at design time.</p></li> <li><p><strong>DestinationRoot</strong> will contain the actual root folder under which the folders like July, August should be created based on the month names. </p></li> <li><p><strong>SourceFolder</strong> denotes the folder in which all the files are initially stored. Here in this example, the source folder will be <code>F:\Temp\</code></p></li> <li><p><strong>SourceFilePath</strong> denotes the actual file path. This variable will be assigned with the individual file values when the Foreach loop container loops through each variable. To avoid the File System Task from throwing error messages at design time, let's assign it with some dummy value <code>F:\Temp\1.txt</code>.</p></li> <li><p><strong>FilePattern</strong> defines the file pattern that should be looped through in the given source folder path. Let's assign <code>*.*</code>, which means all the files will be looped through. You can also specify <code>*.txt</code> or <code>File*.txt</code> or <code>My*.xls</code> etc. It is upto your requirements.</p></li> <li><p><strong>MonthStartPosition</strong> denotes the position where the month value starts in the file name. So, in the file name format <code>File 29-07-2011</code>, the month 07 starts at 9th character. Hence the value 9.</p></li> <li><p><strong>MonthLength</strong> specifies the number of character to extract. This will anyways be 2 characters but I didn't want to hard code. So, I created a variable.</p></li> <li><p><strong>MonthNameFormat</strong> specifies how the folders should be created. Value MMMM denotes that it will create the folders with full month names like January, February etc. If we use the value MMM, the folders will be created as Jan, Feb etc. The folders will be created only if they didn't exist.</p></li> </ul> <p><img src="https://i.stack.imgur.com/yk7b1.png" alt="Variables"></p> <p>On the SSIS package's <strong>Control Flow</strong> tab, place a <code>Foreach loop container</code> and configure it to loop through the folder specified in the variable <code>SourceFolder</code> using the file pattern variable <code>FilePattern</code>. As the Foreach loop container loops through the files, the file names will be assigned to the variable <em>SourceFilePath</em>. We will use this variable to fetch the month value in Script Task.</p> <p><img src="https://i.stack.imgur.com/MYNWq.png" alt="Foreach loop container General"></p> <p><img src="https://i.stack.imgur.com/lTDEe.png" alt="Foreach loop container Collection"></p> <p><img src="https://i.stack.imgur.com/vpzbb.png" alt="Foreach loop container Variable Mappings"></p> <p>Within the Foreach loop container, place a <code>Script Task</code> and on the script task's Script section click the Design script... button to open the VSTA editor and paste the code provided after these screenshots. Since the example was created in VS 2005, the code is written in <strong>VB.NET</strong> because that is the only supported language in <code>SSIS 2005</code>.</p> <p><img src="https://i.stack.imgur.com/5CUZh.png" alt="Script Task General"></p> <p><img src="https://i.stack.imgur.com/RpmI7.png" alt="Script Task Script"></p> <p><img src="https://i.stack.imgur.com/k1FWR.png" alt="Script Task Code"></p> <p><strong>Script Task Code:</strong> The code gets the full file path value from the variable <code>SourceFilePath</code> and extracts only the file name to store it in the local variable <code>FileName</code>. </p> <p>Then checks to see if the <code>MonthStartPosition</code> and <code>MonthLength</code> variables are assigned with proper non-zero values. It then extracts the month value to store it in the local variable <code>MonthValue</code>.</p> <p>Uisng the <code>MonthValue</code>, it fetches the full month name value using the DateTime function. The values 1 are assigned to day and year because we only want the Month name.</p> <p>The month name in the local variable <em>FolderName</em> is combined with the <em>DestinationRoot</em> value to check if the folder exists or not. If the folder doesn't exist, the folder will be created so that the File System Task doesn't fail.</p> <p>Finally the full destination folder value is assigned to the package variable <code>DestinationFolder</code>. This variable will be used in the File System Task.</p> <p><strong><code>VB.NET code for SSIS 2005</code></strong></p> <pre><code>Imports System Imports System.Data Imports System.Math Imports Microsoft.SqlServer.Dts.Runtime Public Class ScriptMain Public Sub Main() Dim varCollection As Variables = Nothing Dts.VariableDispenser.LockForRead("User::SourceFilePath") Dts.VariableDispenser.LockForRead("User::DestinationRoot") Dts.VariableDispenser.LockForRead("User::MonthStartPosition") Dts.VariableDispenser.LockForRead("User::MonthLength") Dts.VariableDispenser.LockForRead("User::MonthNameFormat") Dts.VariableDispenser.LockForWrite("User::DestinationFolder") Dts.VariableDispenser.GetVariables(varCollection) Dim SourceFilePath As String = varCollection("User::SourceFilePath").Value.ToString() Dim FileName As String = SourceFilePath.Substring(SourceFilePath.LastIndexOf("\") + 1) Dim DestinationRoot As String = varCollection("User::DestinationRoot").Value.ToString() Dim MonthStartPosition As Integer = Convert.ToInt32(varCollection("User::MonthStartPosition").Value) Dim MonthLength As Integer = Convert.ToInt32(varCollection("User::MonthLength").Value) Dim MonthValue As Integer = 0 Dim MonthNameFormat As String = varCollection("User::MonthNameFormat").Value.ToString() Dim FolderName As String = String.Empty Dim MonthwiseDirectory As String = String.Empty If MonthStartPosition &gt; 0 AndAlso MonthLength &gt; 0 Then MonthValue = Convert.ToInt32(FileName.Substring(MonthStartPosition - 1, MonthLength)) End If If FileName.Length &gt; 0 AndAlso MonthValue &gt; 0 Then FolderName = New DateTime(1, MonthValue, 1).ToString(MonthNameFormat) End If MonthwiseDirectory = System.IO.Path.Combine(DestinationRoot, FolderName) If Not System.IO.Directory.Exists(MonthwiseDirectory) Then System.IO.Directory.CreateDirectory(MonthwiseDirectory) End If varCollection("User::DestinationFolder").Value = MonthwiseDirectory Dts.TaskResult = Dts.Results.Success End Sub End Class </code></pre> <p><strong><code>C# code for SSIS 2008 and above</code></strong></p> <pre><code>public void Main() { Variables varCollection = null; Dts.VariableDispenser.LockForRead("User::SourceFilePath"); Dts.VariableDispenser.LockForRead("User::DestinationRoot"); Dts.VariableDispenser.LockForRead("User::MonthStartPosition"); Dts.VariableDispenser.LockForRead("User::MonthLength"); Dts.VariableDispenser.LockForRead("User::MonthNameFormat"); Dts.VariableDispenser.LockForWrite("User::DestinationFolder"); Dts.VariableDispenser.GetVariables(ref varCollection); string SourceFilePath = varCollection["User::SourceFilePath"].Value.ToString(); string FileName = SourceFilePath.Substring(SourceFilePath.LastIndexOf('\\') + 1); string DestinationRoot = varCollection["User::DestinationRoot"].Value.ToString(); int MonthStartPosition = Convert.ToInt32(varCollection["User::MonthStartPosition"].Value); int MonthLength = Convert.ToInt32(varCollection["User::MonthLength"].Value); int MonthValue = 0; string MonthNameFormat = varCollection["User::MonthNameFormat"].Value.ToString(); string FolderName = string.Empty; string MonthwiseDirectory = string.Empty; if (MonthStartPosition &gt; 0 &amp;&amp; MonthLength &gt; 0) { MonthValue = Convert.ToInt32(FileName.Substring(MonthStartPosition - 1, MonthLength)); } if (FileName.Length &gt; 0 &amp;&amp; MonthValue &gt; 0) { FolderName = new DateTime(1, MonthValue, 1).ToString(MonthNameFormat); } MonthwiseDirectory = System.IO.Path.Combine(DestinationRoot, FolderName); if (!System.IO.Directory.Exists(MonthwiseDirectory)) { System.IO.Directory.CreateDirectory(MonthwiseDirectory); } varCollection["User::DestinationFolder"].Value = MonthwiseDirectory; Dts.TaskResult = (int)ScriptResults.Success; } </code></pre> <p>Within the Foreach loop container, place <em>File System Task</em> after the Script task. Configure the File System Task as shown in the screenshot.</p> <p><img src="https://i.stack.imgur.com/fvuu0.png" alt="File System Task"></p> <p>Once the package tasks are configured, the Control Flow tab should look like as shown below.</p> <p><img src="https://i.stack.imgur.com/yShdf.png" alt="Control Flow"></p> <p>Let's test the package. Before that, the contents of the source folder F:\Temp are shown below. The files are dummy. Hence, the size 0 KB.</p> <p><img src="https://i.stack.imgur.com/PYNt7.png" alt="F Temp Folder"></p> <p>Below screenshot shows the successful execution of the package.</p> <p><img src="https://i.stack.imgur.com/lt7DM.png" alt="Success"></p> <p>Below screenshots show how the files have been moved to respective destination folder that have been created based on the month names. Contents of the individual folders are shown below.</p> <p>Hope that helps.</p> <p><img src="https://i.stack.imgur.com/BoQyz.png" alt="F Temp"></p> <p><img src="https://i.stack.imgur.com/tY4rP.png" alt="F Temp Monthwise"></p> <p><img src="https://i.stack.imgur.com/jiqtr.png" alt="F Temp Monthwise August"></p> <p><img src="https://i.stack.imgur.com/X3Eiu.png" alt="F Temp Monthwise January"></p> <p><img src="https://i.stack.imgur.com/89TQn.png" alt="F Temp Monthwise July"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload