SSIS: import MAX(filename) from folder

谁都会走 提交于 2020-01-05 04:14:12

问题


I need to pick one .csv file from \\\Share\Folder\ with max filename for further import to SQL. File name is alphanumerical, e.g. ABC_DE_FGHIJKL_MNO_PQRST_U-1234567.csv, where numerical part will vary, but I need the only max one each time the package runs.

Constraints: no write access on that SQL server, I use ##Temp table for import and this is the least desirable method for filename processing (no for each loops on this server).

Ideally it will be function/expr-based variable (combined with script task if needed) to pass into connection manager. Any ideas much appreciated.


回答1:


Use a Script Task

  • Add a variable of type String User::CsvFile
  • Add a script task to your project and add your created variable as a ReadWriteVariable

  • In Your Script task write the following code (VB.NET):

You have to Import System.Linq Library

Public Sub Main()

    Dim strDirectory As String = "C:\New Folder" ' Enter =the directory
    Dim strFile As String = String.Empty

    strFile = IO.Directory.GetFiles(strDirectory, "*.csv", IO.SearchOption.TopDirectoryOnly).OrderBy(Function(x) x.Length).Last


    Dts.Variables.Item("CsvFile").Value = strFile

    Dts.TaskResult = ScriptResults.Success

End Sub
  • Then use this variable from Flat File Source


来源:https://stackoverflow.com/questions/48613952/ssis-import-maxfilename-from-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!