Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 SSIS and Import/Export (2012)
 Traverse through two types of file extensions

Author  Topic 

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2014-05-07 : 02:37:55
Hi All,

By using For Each File Container , how can we loop through only the .txt and .csv files in a folder.

We can specify the below things in "Files" property of "Collection" tab in "Foreach Loop Editor".

For all types of files = *.*
For only txt file = *.txt
For only csv files = *.csv

But my concern is to specify both .txt and .csv files ( don't want to traverse through any other file types except .csv and /txt)


--
Chandu

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-05-09 : 05:54:06
for that you can do in two steps. For ForEachLoop set type as *.*. Add a boolean variable (IsValidFile) to ssis package. Add a script task inside the loop and pass variable in readwrite mode and the filename variable used inside ForEachLoop as ReadOnly. then inside script add a code as below

Dts.Variables("IsValidFile").Value = IIF(InStr(Dts.Variables("FileName").Value,".csv") > 0 Or InStr(Dts.Variables("FileName").Value,".txt") > 0,True,False)


Then link Script task to your subsequent tasks inside the loop, Set precedence constraint option as Expression And Constraint. Set Constraint as OnSucess and Expression as below


@IsValidFile == True



and it will work fine processing only txt and csv files

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2014-05-15 : 00:55:49
Thanks for the detailed explanation...

Can we do this without using Script Task inside ForEachLoop?


--
Chandu
Go to Top of Page
   

- Advertisement -