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

Scenario:

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.

Solution:

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.


25 comments:

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

    ReplyDelete
  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.

    ReplyDelete
  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

    ReplyDelete
    Replies
    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.

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

    ReplyDelete
  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

    ReplyDelete
  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.

    ReplyDelete
  7. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    http://chennaitraining.in/base-sas-training-in-chennai/
    http://chennaitraining.in/abinitio-training-in-chennai/
    http://chennaitraining.in/datastage-training-in-chennai/
    http://chennaitraining.in/cognos-training-in-chennai/
    http://chennaitraining.in/cognos-tm1-training-in-chennai/
    http://chennaitraining.in/microstrategy-training-in-chennai/
    http://chennaitraining.in/qlikview-training-in-chennai/

    ReplyDelete
  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.

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

    ReplyDelete
  10. Hi I have used same above script but getting this error "script task failed due to cannot load script for execution" . Pleas help on this

    ReplyDelete
  11. I have this error on the first line of code:
    String FilePath = Dts.Variables["User::FolderPath."].Value.ToString() + Dts.Variables["User::FileName"].Value.ToString();

    Microsoft.SqlServer.Dts.Runtime.DtsRuntimeException: 'The element cannot be found in a collection. This error happens when you try to retrieve an element from a collection on a container during execution of the package and the element is not there.'

    ReplyDelete
  12. How can you check whether the file exists using a wild card? eg filename*.txt, where the wildcard(*) represents any date ie 20210610?

    ReplyDelete
  13. I Did all this, then started to go insane when I included an excel data source in my data flow, even though I started without a file it kept validating as true and I couldnt work out why.
    Then i worked out what was going on, as part of the prevalidation when the file didnt exist ssis was creating one!! the code above was then passing it as existed but as it wasnt a true file the rest crashed which is what i was trying to avoid... once i got my head around this thanks to google i realised I needed to set "DelayValidation" on the Data Flow to True, now everything is working perfectly!! Thank you.

    ReplyDelete
  14. very clear and helpful! thank you!

    ReplyDelete
  15. Need to check if multiple CSV exists in the same folder or not.
    Please provide steps to create it

    ReplyDelete
  16. Watch and Download world's famous drama series Kurulus Osman in English on link below
    👇
    Kurulus Osman in English

    📢Get high quality backlinks for your
    Website with BacklinksIndexer
    BacklinksIndexer

    Crypto trading online course
    Join on link below
    Crypto quantum leap

    📒 Read Home doctor book online
    Then you will be a doctor for your family
    Home Doctor Book

    💰Create own NFTs and earn 1000$
    Complete guide
    Create NFT

    Join online YouTube course
    And be a professional YouTuber
    Tube Mastery and Monetization by matt

    🦷Steel Bite Pro
    Best product for
    Teeth pain, cavities,teeth whitening and other oral health issues with money back guarantee
    Steel Bite Pro

    ReplyDelete
  17. Welcome To Techbrothersit: Ssis -How To Check If File Exists In Folder [Script Task] >>>>> Download Now

    >>>>> Download Full

    Welcome To Techbrothersit: Ssis -How To Check If File Exists In Folder [Script Task] >>>>> Download LINK

    >>>>> Download Now

    Welcome To Techbrothersit: Ssis -How To Check If File Exists In Folder [Script Task] >>>>> Download Full

    >>>>> Download LINK wm

    ReplyDelete
  18. State Council of Educational Research and Training (SCERT) Jharkhand has Going to Conducted High School Aunval Exam 2023, SCERT Jharkhand which is Mainly Responsible for Conducting 6th, 7th, 8th, 9th, 10th Exam Every year, This SCERT Jharkhand 6th, 7th, 8th, 9th, 10th Exam Very Year More Than 25 Laks of Students Attended, SCERT Jharkhand is Administered by the State Government of Jharkhand and Thrives to Improve the Quality of Education in the state.SCERT Jharkhand is also Responsible for Prescribing the SCERT Jharkhand 6th, 7th, 8th, 9th, 10th Model Paper 2023 for the Students Studying in the Jharkhand Affiliated Schools, JAC Board 8th Question Paper Jharkhand 6th, 7th, 8th, 9th, 10th Question Paper 2023 Designed by Senior Experts Study Materiel help the Students to get an idea about the Final Exam 2023,

    ReplyDelete