Rename a file using SSIS 2008

No Comments

Written by: Luis Miguel – Originally Published on: August 17, 2013

In my previous post I explained how to unzip a file using SSIS. The scenario presented unzipped a single CSV file and the file name was static, so if you executed the package more than once the CSV file would be overwritten.

In today’s post I’m going to present a method to rename the unzipped file so you can preserve the data when the package is executed multiple times.

Steps used to implement this solution

1 – Create a string variable to hold a daily timestamp

The complete package with the new_name variable

The complete package with the new_name variable

2 – Add an Execute SQL Task to populate the variable

Populate the new_name variable

Populate the new_name variable


Enter this T-SQL script to assign a value to the new_file variable
DECLARE @import_timestamp CHAR(8)
DECLARE @new_file VARCHAR(64)
SET @import_timestamp = CONVERT(VARCHAR, GETDATE(), 112)
SET @new_file = ‘c:\unzip\sale_orders_’ + @import_timestamp + ‘.csv’
SELECT @new_file

3 – Add a Script Task component and enter the C# code used to rename the file
The "rename csv file" Script Task

Enter this C Sharp code to rename the file
public void Main()
{
string old_file;
string new_file;
old_file = “c:\\unzip\\sale_orders.csv”;
new_file = Dts.Variables["User::new_name"].Value.ToString();
File.Move(old_file, new_file);
}

Comments are closed.

Powered by Netfirms