Wednesday 19 May 2021

Move the files to the archive folder after loading the data using Script task in SSIS

File system task is used to move the one file from the source to the archive folder at a one time.

Read here:  https://bageshkumarbagi-msbi.blogspot.com/2014/12/file-system-task.html

We have a requirement like after loading the n number of files we need to move these files into the Archive folder.

With the help of script task, we can achieve the same.

Let’s see in this demo

Source Folder   

Archive Folder  

Taking Script task   

Taking variables to store the source and archive folder path.

   

Now in script task we are writing the below code.

public void Main()

              {

            string sourcePath = Dts.Variables["User::Source_dir"].Value.ToString();

            string targetPath =Dts.Variables["User::Archive_dir"].Value.ToString();

             if (!Directory.Exists(targetPath))

            {

                Directory.CreateDirectory(targetPath);

            }

             string[] sourcefiles = Directory.GetFiles(sourcePath);

             foreach (string sourcefile in sourcefiles)

            {

                string fileName = Path.GetFileName(sourcefile);

                string destFile = Path.Combine(targetPath, fileName);

                 File.Move(sourcefile, destFile);

            }

            Dts.TaskResult = (int)ScriptResults.Success;

              }

 We need to add System.IO namespace.

Compile this code saves and close this window.

Now package is ready to run.

 Before running this package see the archive folder.   

Running this package.

                              

Package executed successfully.

See the Archive folder.

  

Files moved to the Archive folder successfully.

1 comment:

  1. This is an exclusive post about PostgreSQL and the use of foreign data wrapper actually overcomes and helps people solve the most complex problems and errors.

    SSIS postgresql read

    ReplyDelete

If you have any doubt, please let me know.

Popular Posts