October 10, 2012 at 12:17 pm
//one FTP connection thru multiple Folders C#
FtpClientConnection ftp_client;
public void Main()
{
ConnectionManager conMan;
String[] sFolderNames;
String[] sFileNames;
String[] sFileNamestoWindowsFolder;
String[] FTPRemoteFolders = { "/FOLDER1", "/FOLDER2", "/FOLDER3"};//,"","",""
try
{
conMan = Dts.Connections["FTP"];
ftp_client = new FtpClientConnection(conMan.AcquireConnection(null));
ftp_client.Connect();
foreach (string folder in FTPRemoteFolders) //THRU FOLDERS LOOP
{
try
{
ftp_client.SetWorkingDirectory(folder);
ftp_client.GetListing(out sFolderNames, out sFileNames);
if (sFileNames != null && sFileNames.GetUpperBound(0) >= 0)
{
ftp_client.ReceiveFiles(sFileNames, Dts.Variables["FTPtoWindowsFolder"].Value.ToString(), true, false);
ftp_client.DeleteFiles(sFileNames);
}
}
catch (Exception ex)
{
Dts.Variables["ErrorMessage"].Value =folder+" -- "+ ""+ex.Message;
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
catch (Exception ex)
{
Dts.TaskResult = (int)ScriptResults.Failure;
Dts.Variables["ErrorMessage"].Value = ex.Message;
Dts.Events.FireError(0, "Get File From FTP Site", ex.Message, String.Empty, 0);
}
finally
{
ftp_client.Close();
}
}
February 23, 2016 at 6:57 am
Here's how I accomplished this.
Create an FTP task. In the File Transfer set Operation to Receive Files and set RemotePath to *.* for all files or *.ext if you only want specific extension.
Then add a Foreach Loop to loop over the local directory to process the files.
After the Foreach Loop add another FTP task. In File Transfer set Operation to Delete remote files and set RemotePath to *.* to remove all files or *.ext to only remove a specific file type.
Viewing 2 posts - 16 through 16 (of 16 total)
You must be logged in to reply to this topic. Login to reply