Here's my folder setup.
Here's the File setup
The idea is to traverse through the folders & put FileA contents to Table FileA.dbo on the database ( also FileB,FileC etc). The FileName structure is same throughout all the folders.
I have this ssis package where I parse through the folders with a foreachloop-> dataflow.
I have checked that the algorithm I've formulated to get the filename is working
REVERSE(Substring(Reverse( @[User::FileName] ),5,LEN( @[User::FileName] ))) == "FileA"
It parses out the .txt extension. Below is the setup of the foreach loop I have.
To start this whole package I've .txt files inside the folder & I make the foreach container loop subfolders.
Since, I'm not really a SSIS package developer, this is the best I could do with some research. The problem I'm running into is, it seems to work partially.
The snapshots are mockups of the scenario I have and in reality I have over 200 folders with 50 text files each designated to dump the contents to respective named tables.
But the total number of rows I'm seeing after the package successfully executes is extremely low & can't be right. Is there anyway to get a count/list of the number of folders it traversed. Also, am I doing something wrong ?
Ideally, I'd just like to not have to start with .txt files but just have the whole thing go to folders, get the filenames ( which I think I've a working code for) & just dump all the info to the OLEDB Destinations.
Any help, link to resources is much appreciated.
Solution overview
You can achieving this using one DataFlow Task inside the foreach loop, but the trick is that you have to Read source flat file name and the Destination SQL Table name from variables
Note: Flat files structure must be the same and SQL Tables must have the same structure
Detailed Solution
- Right Click on the
Control Flow
window and click on Variables
Declare 2 SSIS variables:
FlatFilename
: of type String
and assign a default value a random file path (i.e. C:\MockFolder\FileA.txt
)
- 'SQLTablename
: of type
String` and assign to the following expression:
This is assuming that all destination tables has the same schema dbo
"[dbo].[" + REPLACE(RIGHT( @[User::FlatFilename] , FINDSTRING(REVERSE( @[User::FlatFilename] ) , "\\", 1) - 1),".txt","") + "]"
- Add a
Foreach Loop Container
and a DataFlow Task
inside it, click on the DataFlow Task
and on the properties Tab, Set the Delay Validation
property to True
- Double Click on the
Foreach Loop container
and select the main Directory , and the files filter *.txt
also choose the fully qualified
retrieve file name option
- Go To variable mapping Tab and choose the
@[User::FlatFilename]
variable
Add 2 Connection manager
- FlatFileConnection: a Flat File connection manager, and configure it by selecting randomly a
File
(i.e. C:\MockFolder\FileA.txt
)
- OLEDBConnection: an OLEDB connection manager, and configure it to your Destination SQL Server Database
In The DataFlow Task
, add a Flat File Source
and an OLEDB Destination
, in the OLEDB Destination
select Table name from variable
option and select @[User::SQLTablename]
as the variable name
Map columns between source and Destination
Click on the FlatFileConnection
in the connection manager windows, press F4
to show the properties Tab, click on Expressions
Select the Connection String
property assign to it the following expression:
@[user::FlatFilename]