Demo to download files from FTP location and also delete them afterwards with SSIS Package
July 21, 2015 § Leave a comment
Hi Friends,
Hope you are doing good, while reading this post!
Today, I would like to share the post which gives you the demo to download the files from your FTP location to your local folder and then also delete those remote files from FTP location.
Steps:
- Create a new SSIS Package and go to Control Flow and drag a Script Task component there.
- Create a new FTP Connection and configure
- Create the package variables to save varFTPLocalPath and varRemotePath
- Use these above variables in Script Task to download the file and then delete them afterwards, by using FTP Connection within this task.
Let’s have a look at our FTP location first:
Now, design the SSIS package’s control flow, drag a Script Task and also configure FTP connection manager as below mentioned:
Now, assign the variable’s value as below:
- varFTPLocalPath: “D:\Self\downloadedFromFTP”
- varRemotePath: “ftp://ftp.nonprod.local/TestFiles/”
Then open the Script Task and select the ReadOnlyVariables, as listed below:
Click on “Edit Script” button and write C# code as mentioned below in the code-snippet:
Code-Snippet:
public void Main() { string[] folderArray, fileArray; string fileName = string.Empty, remoteDirectory = string.Empty, localFolderPath = string.Empty; try { ConnectionManager ftpCM = Dts.Connections["FTP_CONN_MGR"]; remoteDirectory = Dts.Variables["varRemotePath"].Value.ToString(); localFolderPath = Dts.Variables["varFTPLocalPath"].Value.ToString(); FtpClientConnection ftpClient = new FtpClientConnection (ftpCM.AcquireConnection(null)); //Connecting to FTP Server ftpClient.Connect(); //Provide the Directory on which you are working on FTP Server ftpClient.SetWorkingDirectory(remoteDirectory); //Get all the files and Folders List ftpClient.GetListing(out folderArray, out fileArray); //If there is no file in the folder, // strFile Arry will contain nothing, so close the connection. if (fileArray == null) ftpClient.Close(); else { foreach (string ftpFile in fileArray) { //Download the file from FTP to your Local Folder string[] downloadFileNameArray; downloadFileNameArray = new[] { remoteDirectory + "/" + ftpFile }; ftpClient.ReceiveFiles(downloadFileNameArray, localFolderPath, true, true); //Delete the file from REMOTE location (i.e. FTP location) ftpClient.DeleteFiles(downloadFileNameArray); } // Close the FTP Client ftpClient.Close(); } Dts.TaskResult = (int)ScriptResults.Success; } catch (Exception ex) { Dts.TaskResult = (int)ScriptResults.Failure; throw ex; } }
Now, build your script task code and close this and click on OK button of Script Task Editor window.
Now, build your solution and run your package.
That’s it, now check for your destination or local folder and you will find that you got all the files downloaded from the FTP location and also observe that those files have been deleted from your FTP location.
That’s it, you are done!
Please share this post, if you find this useful and any suggestions will be highly appreciated!
Happy Coding 🙂