Pages

Thursday, December 19, 2013

SSIS - How to check if a File exists

Scenario:

We have a scenario, in which we have to check if a file exists in a folder. 

If the file exists , delete it and if the file does not exists, create a new one and append some data to it. 

Solution:

We can achieve this, using a simple script in "Script Task" in the Control Flow. 

Lets say, 
File name: Commands.bat 
File Path: I:\Commands.bat

Moving on to how to achieve this.

Step 1: 

Create the following variables at package level.

v_fileName = "Commands.bat" (Give the name of the file that you are checking for)
v_filePath = "I:\" (Give the folder where the file has to be located at)


Step 2: 

Drag and Drop the Script task from the tool box to the work space(control flow) and click on the bubble for ReadOnlyVariables.






Step 3: 

select the input variables for the Script task as shown below. 


Click on OK and then click on Edit Script



Step 4: 

Copy the code which is shown below



 Step 5: 

Execute the task to see the file created. 
Before Executing task





After Executing ScriptTask


No comments:

Post a Comment