问题
I have a folder having multiple files with the name as
P04_20140326_1234.zip
P04_20130324_58714.zip
P04_20130808_jurhet.zip
P04_20130815_85893.zip
etc
The name is in the format P04_systemdate_*.zip.
I want to pick the folder containing currentdate in the name and unzip it first and load the data from extracted file into the table.eg : file named as A.txt goes into table A, filenamed as B goes into table B and so on...
回答1:
I guess you have already done the following:
- Add a
Data Flow
- Inside the data flow, add a
flat file source
, andOle_DB destination
- Configure the
flat file source
to point to one of your files and connect all the appropriate columns so that data flows from file to database.
If all of this is already working, then let's do the For-Each loop
- Create a variable (default to package root level) and call it
CsvFileName
of type string - Add a
ForEach loop
(not a For loop) - Change loop type to be a
Foreach File Enumerator
- Set your folder path and look for *.csv
- Under Variable mappings, add the variable "User::CsvFileName" variable, and set the index to 0 - this means that all file names returned from the Foreach loop will show up in the variable.
- In the Connection Managers (bottom) right click on the
FlatFileSource
, and choose properties - Set the
DelayValidation
to "True" - Click on Expressions, and then click on the ellipsis
- Set the
ConnectionString
property to use the "CsvFileName" variable
Run it. This should load all files. Now, if you just want to restrict it to a date here's what you do:
- Create a variable called "FilterDate"
- Set the value to whichever date you want to set (20140322, for example)
- In the ForEach loop, go to Collections, and then click on Expressions, then click the ellipsis
- Set the FileSpec property to be
"*" + @[User::FilterDate] + "*.csv"
Now it will only filter the files that you want.
来源:https://stackoverflow.com/questions/22672468/ssis-foreach-loop-container-dynamic-file-name-and-path-then-unzip-files