Search code examples
sql-serverssisetlssis-2012

filter filename from folder that contains a specific text in ssis


I am trying to move some files from one folder to another, and i Need to evaluate if a file name contains certain text then only we need to move those files.

for example , i have following files in a folder.

abcd_takeme_fdsljker.txt
abcd_file_fdsljker.txt
abcd_takeme_fdsljsdfker.txt
abcd_filetk_fdsljker.txt
abcd_takeme_fdsljssker.txt

from the above I want to pick files which has text "takeme"


Solution

  • Using For each loop container

    1. You have to add a for-each loop container to loop over files in a specific directory.

    2. Choose the follow expression as a filename:

      *takeme*

    3. Map the filename to a variable

    4. Add a dataflow task inside the for each loop to transfer files
    5. use the filename variable as a source

    you can follow the detailed article at:

    if you want to add multiple filter follow my answer at:

    Using a script task

    or you can achieve this using a script task with a similar code: (i used VB.Net)

    Public Sub Main()
    
        For Each strFile As String In IO.Directory.GetFiles("C:\New Folder\", "*takeme*", IO.SearchOption.AllDirectories)
    
            Dim filename As String = IO.Path.GetFileName(strFile)
    
            IO.File.Copy(strFile, "D:\New Folder\" & filename)
    
        Next
    
        Dts.TaskResult = ScriptResults.Success
    End Sub