SSIS - Dynamically moving Files to Folder with Matching Substring Name

不问归期 提交于 2019-12-23 18:13:46

问题


I'm using a foreach loop and file system task to move files into specific folders (or trying to at least).

i.e.

A file name can be 100000 and it needs to go to folder 1000 File 102000 needs to go to folder 1020 File 103000 need to go to folder 1030 etc etc

I'm struggling with how to move the files to the correct folder.

I thought I could use a variable with the upper level directory followed by a substring of the filename variable held in the foreach loop

e.g.

"D:\\Archive\\" + SUBSTRING(@[USER::Variable],1,4)

But that doesn't work and I get an error that the path format is not supported.

Any help is greatly appreciated, thanks.


回答1:


Your issue

First of all,I think the error is because the variable contains the fullpath not only the filename so you have to use a similar expression:

"D:\\Archive\\" + LEFT(RIGHT( @[User::Variable] , FINDSTRING(REVERSE( @[User::Variable] ) , "\\", 1) - 1),4)

Detailed Solutions

First Method - Using script Task

Try using a script task to achieve this, Just select your variable as a ReadOnlyVariable in the Script Task. and use a similar script (I Used Vb.net)

Public Sub Main()

    Dim strFile As String = Dts.Variables.Item("User::Variable").ToString

    Dim strFilename As String = IO.Path.GetFileName(strFile)


    'Create Directory
    If Not IO.Directory.Exists("D:\Archive\" & strFilename.Substring(0, 4)) Then
        IO.Directory.CreateDirectory("D:\Archive\" & strFilename.Substring(0, 4))
    End If

    'Copy File to destination
    IO.File.Copy(strFile, "D:\Archive\" & strFilename.Substring(0, 4) & "\" & strFilename)

    Dts.TaskResult = ScriptResults.Success
End Sub

Second Method - Using File System Task

Create a new Variable @[User::DestinationPath] and set it's property Evaluate As Expression = True, then use the following expression for it:

"D:\\Archive\\" + LEFT(RIGHT( @[User::Variable] , FINDSTRING(REVERSE( @[User::Variable] ) , "\\", 1) - 1),4)

Variable Screenshot

File System Task



来源:https://stackoverflow.com/questions/44591799/ssis-dynamically-moving-files-to-folder-with-matching-substring-name

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