Scenario:We often have the requirement in which we need to delete the Folder from FTP Server. FTP Task have the operation in which we can Delete the Remote Folder but if FTP Server Folder ( Remote Folder) has the files, FTP Task will fail with below error.
"Error: 0xC001602F at DeleteDirectoryWithFiles, Connection manager "FTPConnection": Deleting folder "/FolderName" ... 550 The directory is not empty. "
How can we delete the folder with Files on FTP Server?
Solution:There could be multiple solutions for this requirement. We can use Script task and check if the files exists in Remote Folder or not. If file/s exists then we can use First FTP Task to Delete all the files and second FTP Task to Delete the Folder.
If no file exits then we have to direct our flow to second FTP Task that will delete the remote folder.
Instead of using above solutions, we can simply use the Script Task and handle everything in it. Below code can be used to drop the Directory with or Without Files on FTP Server.
Public Sub Main() Dim StrFolderArrary As String() Dim StrFileArray As String() Dim RemoteDirectory As String RemoteDirectory = Dts.Variables("User::RemoteFolder").Value.ToString() 'FTP connection manager name
Dim cm As ConnectionManager = Dts.Connections("FTPConnection") Dim ftp As FtpClientConnection =
New FtpClientConnection(cm.AcquireConnection(Nothing)) ftp.Connect() 'Connecting to FTP Server 'Provide the Directory that we want to drop ftp.SetWorkingDirectory(RemoteDirectory) 'Get all the files and Folders List
ftp.GetListing(StrFolderArrary, StrFileArray) 'If there is no file in the folder, strFile Arry will contain nothing, so close the connection. 'and delete the directory If StrFileArray Is Nothing Then ftp.DeleteRemoteDirectory(RemoteDirectory) 'If Remote Directory has the files then delete the files first and then delete the directory Else ftp.DeleteFiles(StrFileArray) ftp.DeleteRemoteDirectory(RemoteDirectory) ftp.Close() End If ' Add your code here ' Dts.TaskResult = ScriptResults.Success End Sub
Delete Directory with Files from FTP Server in SSIS Package by using Script Task
Related Posts / Videos on FTP Task / Script Task
- FTP Task - How to Upload Single File to FTP Server from Local Folder in SSIS Package
- FTP Task - How to Download Single File from FTP Server to Local Folder in SSIS Package
- FTP Task - How to Upload Multiple Files from Local Folder To FTP Server Folder in SSIS Package
- FTP Task - How to Download All the files from FTP Server Folder to Local Folder in SSIS Package
- FTP Task - Filter Files by using WildCard in FTP Task in SSIS Package for downloading
- FTP Task - Delete Specific Files or All Files from FTP Server Folder by using SSIS Package
- FTP Task - Download Only Current Day Files from FTP Server by using FTP Task in SSIS Package
- FTP Task - Create A Directory Folder For Each Day and Load Files on FTP Server by using SSIS Package
- Get File Names from FTP Server and Save to SQL Server Table in SSIS Package by using Script Task
- FTP Task and Script Task - How to Avoid FTP Task error when no file found on FTP Server
- FTP Task and Script Task - Download a File from FTP Site and Delete after Download in SSIS Package
- FTP Task and Script Task - Sync Local Folder to FTP Server Folder without Upload Existing Files by SSIS Package
- Sync FTP Folder to Local Folder without Downloading Existing Files by using Script Task in SSIS Package
- FTP Task/Script Task - Rename File on FTP Server After Downloading in SSIS Package
- FTP Task/Script Task -How to move file from one folder to another folder on FTP server by using SSIS Package
- FTP Task - Create Local Folder with Date on Daily basis and load files from FTP Folder in SSIS Package
- FTP Task - How to Save Password,User Name,Ftp Server Name as variables/Parameters in SSIS Package to make SSIS Package Dynamic to run in SIT, UAT, QA and Prod.