SSIS -How to Check IF File Exists In Folder [Script Task]


Sometime we get requirement something like this: Your Package should check a flag (Cell Value) in table and if it is 1 then run the remaining tasks in Package OR Check I flag file exists in Folder then run the remaining Tasks in Package.


Let’s say in this scenario, we are considering a third party software runs on some schedule and once it get completed it create a file in Folder C:\temp with name ProcessCompleted.txt.
What we have to do, create a package in which the very first step should be to check if file exists. If file exists then we will run our other tasks otherwise we can complete our package without running any tasks.

Step 1:
Create three variables in your SSIS Package
FolderPath  :: provide the folder path in which you want to check the file
FileName : : provide the file name
FileExistsFlg :: we will use this to indicate file exists or not. 1 means exists and 0 means does not exists.

 Step 2: 
After creating variables, Drag Script Task to the Control Flow Pane and Provide Variables and Finally Click on Edit Script.

Step 3:
Write the code as shown in snapshot, We are checking if the file exits in the folder then we are setting the value of FileExistsFlg variable to 1 else it will remain 0.

Step 4:
As now we have set the value of variable FileExistsFlg, we can use this variable to run our next tasks if file exists (FileExistsFlg=1). Bring Data Flow task in Control Flow Pane and Connect Script Task to it. Double click on Green line ( Precedence Constraint) between two tasks.
Choose Expression and Constraint from Evaluation operation drop down and in Value choose "Success". In expression write @FileExistsFlg==1.

This mean whenever the file will be available in given folder , our package will execute and it will run Data Flow task. If file will not available it will stop after executing Script task. As to run Data file the variable ( FileExistsFlg should be 1.

Final Output

As in snapshot we can see that when the file will be present, the data flow task will run otherwise it will stop at Script task.


  1. i did this last week with different approach but your approach is the best. Thanks again.

  2. You are welcome! We can use ForEach loop and then inside we can always precedence constraints to make further decision what to do if file name is matching with our criteria.

  3. Do you have an example of the loop code as I want to loop through it mulitply times if the file does not arrive save for 60- 70 minutes every 5 minutes

    1. Do this with a SQL Agent job or with a Trigger in Azure Data Factory... I would prefer to just execute the package every 5 minutes over leaving a package running for 60-70 minutes.

  4. How to do the above task if filename has date at the end and it changes everyday

  5. You there, this is really good post here. Thanks for taking the time to post such valuable information. Quality content is what always gets the visitors coming. script cek mutasi

  6. Hello! I loved your blog. I've been learning VS a lot from your videos. Thanks a lot. I wanted to ask you, I need to develop what you have done but my FileName varies. I've tried naming my FileName as "*csv" because is a csv extension, but it hasn't worked. Can you help me with this? Thanks a lot in advance.

  7. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...

  8. Nice code, I did apply, but to be clear, does the FileExistsFlg initializes to zero after each foreach loop run. It appears it does because in the coding part you have if condition and no else condition to make FileExistFlg to 0.
    But still wanted to confirm.

  9. if fileexists Flag is an Boolean type expression then How to get the Variable value