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.
PublicSub Main()
Dim StrFolderArrary AsString()
Dim StrFileArray AsString()
Dim RemoteDirectory AsString
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 IsNothingThen
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()
EndIf
' Add your code here
'
Dts.TaskResult = ScriptResults.Success
EndSub
Delete Directory with Files from FTP Server in SSIS Package by using Script Task