3

I have a process built in SSIS that loops through Excel files and Import data only from those that include name Report.

My UserVariable used as Expression is: *Report*.xlsx and it works perfectly fine. Now I am trying to build similar loop but only for files that DOES NOT include Report in file name.

Something like *<>Report*.xlsx

Is it possible?

Thanks for help!

Matt

Hadi
  • 36,233
  • 13
  • 65
  • 124
Mateusz Konopelski
  • 1,000
  • 4
  • 20
  • 37
  • Duplicate question. This works. Just tried it. http://stackoverflow.com/questions/19093008/ssis-exclude-certain-files-in-foreach-loop-container – manderson Mar 06 '17 at 17:01
  • @manderson this is not a duplicate. It is some kind of workaround – Hadi Mar 06 '17 at 17:43
  • I said duplicate, because the answer is the same. I didn't want to basically copy that answer into this question, but since it's not a duplicate... I put the answer in for the user. – manderson Mar 06 '17 at 18:13
  • The OP was asking if he can achieve this using Expressions. My remark was on the word `duplicate` not the whole comment. The link you provided is helpful for this case. – Hadi Mar 06 '17 at 20:50

2 Answers2

2

In your loop, put a Script task before your first task. Connect those two with a line. Right click that line and set Constraint Options to expression. Your expression would look like this...

FINDSTRING(@var, "Report", 1) == 0

Where @var is the loop iterable.

Only files without "Report" inside will proceed to the next step.

Referencing this exact answer. SSIS Exclude certain files in Foreach Loop Container

Community
  • 1
  • 1
manderson
  • 837
  • 1
  • 6
  • 18
1

Unfortunately, you cannot achieve this using SSIS expression (something like *[^...]*.xlsx) you have to search for some workarounds:

Workarounds

First

Get List of - filtered - files using an Execute Script Task before entering Loop and loop over then using ForEach Loop container (Ado enumerator)

  1. You have to a a SSIS variable (ex: User::FilesList) with type System.Object (Scope: Package)
  2. Add an Execute Script Task befor the for each Loop container and add User::FilesList as a ReadWrite Variable
  3. In the Script Write The following Code:

    Imports System.Linq Imports System.IO Imports System.Collections.Generic

    Public Sub Main()
        Dim lstFiles As New List(Of String)
        lstFiles.AddRange(Directory.GetFiles("C:\Temp", "*.xlsx", SearchOption.TopDirectoryOnly).Where(Function(x) Not x.Contains("Report")).ToList)
    
        Dts.Variables.Item("FilesList").Value = lstFiles
    
        Dts.TaskResult = ScriptResults.Success
    End Sub
    
  4. In the For each Loop Container Choose the Enumertaion Type as 'From variable Enumerator' and choose FilesList variable as a source

ScreenShots

enter image description here

enter image description here

enter image description here

Second

Inside the for each loop add an Expression Task to check if the file contains Report string or not

  1. Add a variable of type System.Boolean (Name: ExcludeFile)
  2. Inside the ForEach Loop Container add an Expression Task component before the DataFlowTask you that imports the Excel File

enter image description here

  1. Inside The Expression Task write the following:

     @[User::ExcludeFile]  = (FINDSTRING(@[User::XlsxFile], "Report", 1 ) == 0)
    

enter image description here

  1. Double Click on the connector between the expression task and the DataFlowTask and write the following expression

    @[User::ExcludeFile] == False
    

enter image description here

Note: It is not necessary to use an Expression Task to validate this you can use a Dummy DataFlowTask or a Script Task to check if the filename contains the Keyword you want to exclude or not

Hadi
  • 36,233
  • 13
  • 65
  • 124