SSIS - Archive Files And Add Timestamp To Each File Name [How To Use File System Task]


There are different types of files we have in our Source folder. We want to archive them to Archive folder. While archiving them we want to add time-stamp with  each file name.

Solution :

We will be using For-each loop and File System task to perform this task. Let's start with our step by step approach

Step 1: 
Create two variable VarSourceFolder and VarArchiveFolder and provide the values

Step 2: 
Bring For-each Loop container to Control Flow Pane and configure as shown below. I used the VarSourceFolder variable as Directory.  If we need to change Source Folder Path,We do not have to open the package , Instead we can just change the value for this variable by using SSIS Configuration.

The Information we want to extract is File Name and extension , Choose that.

Create variable VarFileName to hold the FileName and extension
The For-each loop is loop through the folder files one by one and on each iteration file name and extension will be saved in VarFileName variable.

Step 3: 
Create a new variable VarArchiveFullPath, Click on that and press F4, that will take you to properties. Go to expressions and paste below expression

@[User::VarArchiveFolder]+"\\"+ Replace(Replace(Replace(Replace( @[User::VarFileName],".","_"+Substring((DT_WSTR,50) GETDATE(),1,19)+"."),"-",""),":","")," ","_")

Set EvaluteAsExpression=True

Step 4:
Create a variable VarSourceFullPath, this will have Source Folder path and file name together. Write expressions on it as shown below

Step 5:
Bring File System task to Control Flow Pane and configure as shown below

Final Output:
Let's run our SSIS Package and see if files are moved and renamed.

As we can see that all the files are moved from Source Folder to Archive Folder successfully and Time-Stamp is also added.