Problem:
Create a directory based on each year in a specified path, and
then create a flat file named with compound of year and month based on a
template file. You need to transfer some data into this flat file from a
source.
The above scenario is a usual scenario when you work with
flat files,
for example creating mainframe files. Challenges in this scenario are:
- creating file and folder name dynamically
based on variables - Create a Flat file
connection is SSIS based on variable name
Solution:
I suppose that you know everything about File System Task
and you can use it in a SSIS package correctly.
- Create a Script Task in
your SSIS package name it “set folder path” - Use “DirPath” (variable
which maintain specified path for working folder) and “Year”(variable which has
year in value, suppose this year set from outside of SSIS package) as
ReadOnlyVariables. - Use “DestinationPath” as
ReadWriteVariables. Path of generated folder will be stored here. - Write this script in Main()
method: Dts.Variables["User::DestinationPath"].Value =
Dts.Variables["User::DirPath"].Value.ToString()
+ "\\" + Dts.Variables["User::Year"].Value.ToString(); - Create a File System Task
and name it “create folder” - Use “DestinationPath” as
source connection variable - Create a File system Task
and name it “copy file” - Use “SourceFilePath”
(variable which has path of source template file) as source, and “DestinatioPath”
as destination variable - Create a Script Task and
name it “Set File Name” - Use “DestinationPath” and “Year”
and “Month” as ReadOnlyVariables - Use “FilePathAfterRename”
(for file path after renaming) and “FilePathBeforeRename” (for file path before
renaming) as ReadWriteVariables. - Write this script in Main() method: Dts.Variables["User::FilePathBeforeRename"].Value
=Dts.Variables["User::DestinationPath"].Value.ToString()+"\\base.txt"; - Dts.Variables["User::FilePathAfterRename"].Value =
Dts.Variables["User::DestinationPath"].Value.ToString()
+ "\\" + Dts.Variables["User::Year"].Value.ToString() +
Dts.Variables["User::Month"].Value.ToString()+".txt"; - Create a File System Task
and name it “rename file” - Set operation as Rename
File and set “FilePathBeforeRename” as source variable and use “FilePathAfterRename”
as destination variable. - Create a Data Flow Task
- Create a Flat File
connection with source of your template file and configure it - Go to Flat file connection
properties window in expression and set connectionString with @[User::FilePathAfterRename] - Use this Flat file
connection in a Flat File Destination in DataFlow and transfer your data there. - Run SSIS package
Two challenges above solved by:
- Using Script Task with File
System Task for generating dynamic file and folder names - Set connectionString in
expression property of Flat File Connection Manager