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





