Search code examples
ssisforeach-loop-container

SSIS Validate a Foreach File Enumerator


i have a ssis project, to start with, i have a Foreach File Enumerator and inside it are multiple tasks. It works flawlessly under normal circumstances, however if there is no file present in the enumerator directory, an error occurs, the question is, how can i make a validation or some sort for it to avoid it throwing an error on runtime? thanks

img links

ssis http://s15.postimage.org/l41py15aj/ssis.png error http://s15.postimage.org/rj0qupc0b/ssiserror.png


Solution

  • You can have a script task before Foreach loop which basically checks for files and directory .Then have a precedence constraint to halt the package if either directory or files is not present .

    Create 3 variables

     Name        DataType   
     Directory   String
     Files       String
     Exists      int
    

    In the script task just check if the directory is present along with the files .You can modify the code if you need to even enumerate the subfolders or check if specific files are present in the folder

    if (Directory.Exists(Dts.Variables["User::Files"].Value.ToString()))
    {
    if (Directory.GetFiles(Dts.Variables["User::Files"].Value.ToString()).Length != 0)
        {
          Dts.Variables["User::Exists"].Value = 1;
         }
                else
                {
                    Dts.Variables["User::Exists"].Value = 0;
                }
            }
            else 
            {
                Dts.Variables["User::Exists"].Value = 0;
            } 
    

    In the precedence constraint check the value for the variable Exists

        Evaluation operation : Expression
        Expression           : @Exists==1 
    

    Update :

    In the script task editor you need to add the variables in the ReadOnlyVariable section in the script tag