Answered by:
Count no of files in FTP server

Question
-
Hi Experts,
I have a FTP server and i would like to count the files with in ftp server and map that count with ssis in script task .
Can help me with c# code
and i must need to supply username and password with the code
Thanks in advance
ADKR
Friday, June 10, 2016 10:26 AM
Answers
-
No reason to reinvent the wheel. I found an FTP class on Code Project. So with that code, I just created an assembly, installed it to the GAC, referenced it in the script task, and then performed my custom tasks, outlined in the code block below.
string[] AssessorFolderList = new string[] { "prop_characteristics", "PlatList", "Assessor_roll/MS_Access" }; string DestinationFolder = @"E:\CountyData\"; ftp FtpClient = new ftp(@"ftp://ftp.****.***/assessor", "Anonymous", string.Empty); foreach (string DirName in AssessorFolderList) { string[] SimpleDirectoryListing = new string[1]; switch (DirName) { case "prop_characteristics": SimpleDirectoryListing = FtpClient.DirectoryListSimple(DirName).Where(e => e.EndsWith("AV.txt")).ToArray(); break; case "PlatList": SimpleDirectoryListing = FtpClient.DirectoryListSimple(DirName).Where(e => e.Contains("PlatList") && e.EndsWith("xlsx")).ToArray(); break; case "Assessor_roll/MS_Access": SimpleDirectoryListing = FtpClient.DirectoryListSimple(DirName).Where(e => e.EndsWith("zip")).ToArray(); break; } for (int i = 0; i < SimpleDirectoryListing.Count(); i++) { Console.WriteLine("Downloading file....{0}", SimpleDirectoryListing[i].ToString()); FtpClient.Download(string.Format("{0}/{1}", DirName, SimpleDirectoryListing[i]), string.Format("{0}{1}", DestinationFolder, SimpleDirectoryListing[i])); Console.WriteLine("Completed Downloading....{0}", SimpleDirectoryListing[i].ToString()); if (SimpleDirectoryListing[i].EndsWith("zip")) { FtpClient.Decompress(DestinationFolder, SimpleDirectoryListing[i]); } } SimpleDirectoryListing = null; } FtpClient = null;
- Proposed as answer by Seif Wang Tuesday, June 28, 2016 11:30 AM
- Marked as answer by Eric__Zhang Thursday, June 30, 2016 9:13 AM
Friday, June 10, 2016 10:01 PM
All replies
-
Greetings Dileep,
Please refer to http://stackoverflow.com/questions/4584789/connecting-ftp-server-with-credentials.
As for code-example, please check the Stackoverflow or MSDN-arcticels first.When you got a certain part in which you don't understand the logic,the way its executed or you require additional functional... please refer to the forums with a question.
Sebastian
Friday, June 10, 2016 10:56 AM -
here is code ,i am not expert in C#,can any one edit the code for me
public void Main()
{
string userName = Dts.Variables["User::UserName"].Value.ToString();
string password = Dts.Variables["User::PassWord"].Value.ToString();
string fileName = Dts.Variables["User::FileName"].Value.ToString();
string ftpURL = String.Format("ftp://ftp.Myftp.com/{0}",fileName);\\\we dont know what file name is,if any file is present IsFileExists variable must be true,else it should be FALSE
try
{
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpURL);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.Credentials = new NetworkCredential(userName, password);
using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse())
{
Dts.Variables["User::IsFileExists"].Value = true;
}
}
catch
{
Dts.Variables["User::IsFileExists"].Value = false;
}
Dts.TaskResult = (int)ScriptResults.Success;
}ADKR
Friday, June 10, 2016 11:36 AM -
No reason to reinvent the wheel. I found an FTP class on Code Project. So with that code, I just created an assembly, installed it to the GAC, referenced it in the script task, and then performed my custom tasks, outlined in the code block below.
string[] AssessorFolderList = new string[] { "prop_characteristics", "PlatList", "Assessor_roll/MS_Access" }; string DestinationFolder = @"E:\CountyData\"; ftp FtpClient = new ftp(@"ftp://ftp.****.***/assessor", "Anonymous", string.Empty); foreach (string DirName in AssessorFolderList) { string[] SimpleDirectoryListing = new string[1]; switch (DirName) { case "prop_characteristics": SimpleDirectoryListing = FtpClient.DirectoryListSimple(DirName).Where(e => e.EndsWith("AV.txt")).ToArray(); break; case "PlatList": SimpleDirectoryListing = FtpClient.DirectoryListSimple(DirName).Where(e => e.Contains("PlatList") && e.EndsWith("xlsx")).ToArray(); break; case "Assessor_roll/MS_Access": SimpleDirectoryListing = FtpClient.DirectoryListSimple(DirName).Where(e => e.EndsWith("zip")).ToArray(); break; } for (int i = 0; i < SimpleDirectoryListing.Count(); i++) { Console.WriteLine("Downloading file....{0}", SimpleDirectoryListing[i].ToString()); FtpClient.Download(string.Format("{0}/{1}", DirName, SimpleDirectoryListing[i]), string.Format("{0}{1}", DestinationFolder, SimpleDirectoryListing[i])); Console.WriteLine("Completed Downloading....{0}", SimpleDirectoryListing[i].ToString()); if (SimpleDirectoryListing[i].EndsWith("zip")) { FtpClient.Decompress(DestinationFolder, SimpleDirectoryListing[i]); } } SimpleDirectoryListing = null; } FtpClient = null;
- Proposed as answer by Seif Wang Tuesday, June 28, 2016 11:30 AM
- Marked as answer by Eric__Zhang Thursday, June 30, 2016 9:13 AM
Friday, June 10, 2016 10:01 PM