Tuesday, December 27, 2016

How to download FTP files using .NET Interop

download-1459071_1920_thumb

Recently i have worked on a project where the requirement was to upload and download the files from a FTP.  In this blog i will be explaining how to download the files using .NET Interop. There are several other blogs which have explained, how to download the files using ScriptingHost or .net interop but i have not found an example to download all the files from a particular folder.

These are couple of blogs i found, related to this.

http://www.archerpoint.com/blog/Posts/automating-command-line-functions-nav-rtc-transmit-ftp

http://www.dynamics.is/?p=583

Below is the code that downloads all the files from a particular FTP Folder to your local download folder. The GetFTPSetup is just another function to retrieve the setup values.

I have used two functions to download the files, the first function DownloadFilesFromFTP will find all the files and add to the list, then we will loop through the list to download the file using the second function.


PROCEDURE DownloadFilesFromFTP@1240060028();
     VAR
       FTPWebRequest@1240060000 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebRequest";
       FTPWebResponse@1240060001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebResponse";
       NetworkCredential@1240060002 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.NetworkCredential";
       WebRequestMethods@1240060003 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebRequestMethods+File";
       UTF8Encoding@1240060004 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.UTF8Encoding";
       ResponseStream@1240060005 : InStream;
       FileStream@1240060006 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileStream";
       TempBlob@1240060008 : TEMPORARY Record 99008535;
       FileName@1240060007 : Text;
       OutStream@1240060009 : OutStream;
       StreamReader@1240060010 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.StreamReader";
       List@1240060012 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.Generic.List`1";
       i@1240060011 : Integer;
       PathHelper@1240060013 : DotNet "'mscorlib'.System.IO.Path";
       FileAttributes@1240060014 : DotNet "'mscorlib'.System.IO.FileAttributes";
       DotNetFile@1240060015 : DotNet "'mscorlib'.System.IO.File";
     BEGIN
       GetFTPSetup;
       FTPWebRequest := FTPWebRequest.Create(FTPSetup."FTP Download FilePath");
       FTPWebRequest.Credentials := NetworkCredential.NetworkCredential(FTPSetup."FTP UserName",FTPSetup."FTP Password");
       FTPWebRequest.UseBinary := TRUE;
       FTPWebRequest.UsePassive := TRUE;
       FTPWebRequest.KeepAlive := TRUE;
       FTPWebRequest.Method := 'NLST';
       FTPWebResponse := FTPWebRequest.GetResponse();
       StreamReader := StreamReader.StreamReader(FTPWebResponse.GetResponseStream());
       List := List.List();
       FileName := StreamReader.ReadLine();
       WHILE FileName <> '' DO BEGIN
         List.Add(FileName);
         FileName := StreamReader.ReadLine();
       END;
       FOR i:=1 TO List.Count DO BEGIN

        IF FORMAT(List.Item(i-1)) <> 'archive' THEN BEGIN
           DownloadFileFromFTP(FORMAT(List.Item(i-1)));
         END;
         
       END;
     END;

    [TryFunction]
     LOCAL PROCEDURE DownloadFileFromFTP@1240060029(FileToDownload@1240060010 : Text);
     VAR
       FTPWebRequest@1240060000 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebRequest";
       FTPWebResponse@1240060001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebResponse";
       NetworkCredential@1240060002 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.NetworkCredential";
       WebRequestMethods@1240060003 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebRequestMethods+File";
       UTF8Encoding@1240060004 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.UTF8Encoding";
       ResponseStream@1240060005 : InStream;
       FileStream@1240060006 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileStream";
       TempBlob@1240060008 : TEMPORARY Record 99008535;
       FileName@1240060007 : Text;
       OutStream@1240060009 : OutStream;
     BEGIN
       GetFTPSetup;
       FTPWebRequest := FTPWebRequest.Create(FTPSetup."FTP Download FilePath" + FileToDownload);
       FTPWebRequest.Credentials := NetworkCredential.NetworkCredential(FTPSetup."FTP UserName",FTPSetup."FTP Password");
       FTPWebRequest.UseBinary := TRUE;
       FTPWebRequest.UsePassive := TRUE;
       FTPWebRequest.KeepAlive := TRUE;
       FTPWebRequest.Method := 'RETR';
       FTPWebResponse := FTPWebRequest.GetResponse();
       ResponseStream := FTPWebResponse.GetResponseStream();
       TempBlob.Blob.CREATEOUTSTREAM(OutStream);
       COPYSTREAM(OutStream,ResponseStream);
       FileName := FTPSetup."FTP Local Download FilePath" + FileToDownload; // 'download' + FORMAT(CURRENTDATETIME,0,'<Year4><Month,2><Day,2><Hours24><Minutes,2><Seconds,2>') +'.txt';
       TempBlob.Blob.EXPORT(FileName);
     END;

Please leave your comments, feedback or any suggestions you have for me to improve my blog and also if you have any questions, feel free to post.


52 comments:

  1. I wish to indicate because of you only to bail me out of this specific trouble.As a consequence of checking through the net and meeting systems that were not beneficial, I thought my life was finished

    Java Training in Bangalore|

    ReplyDelete
  2. Your good knowledge and kindness in playing with all the pieces were
    very useful. I don’t know what I would have done if I had not
    encountered such a step like this.

    Selenium Training in Bangalore

    ReplyDelete
  3. Appreciating the persistence you put into your blog and detailed information you provide.
    Besant technologies Marathahalli

    ReplyDelete
  4. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, have a nice weekend!
    rprogramming training in bangalore

    ReplyDelete
  5. Thanks a lot very much for the high your blog post quality and results-oriented help. I won’t think twice to endorse to anybody who wants and needs support about this area.
    rprogramming training in bangalore

    ReplyDelete
  6. Informative blog... Thnq for sharing your information with us ...Continue more

    Selenium training in Velachery | Selenium training in chennai

    ReplyDelete
  7. How to delete a from FTP server.

    ReplyDelete
  8. Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
    Good discussion.
    CCNA Training in Chennai
    DevOps Training in Chennai
    DevOps certification Chennai
    DevOps certification
    CCNA Training
    CCNA courses in Chennai

    ReplyDelete
  9. Excellent post. I am really impressed the way you have written the article. I have gained some insight knowledge about the topic

    Linux Course in Chennai | Linux Training | Learn Linux | Linux Course in Adyar | Linux Course in Tambaram | Linux Course in Velachery

    ReplyDelete
  10. Brilliant ideas that you have share with us.It is really help me lot and i hope it will help others also.update more different ideas with us.
    german classes in bangalore
    german language course in bangalore
    German Training in Thirumangalam
    German Training in Nungambakkam

    ReplyDelete
  11. Your blog is so inspiring for the young generations.thanks for sharing your information with us and please update more new ideas.
    AWS Certification Training
    AWS Training Institutes in Bangalore
    AWS Training in anna nagar
    AWS Training in T nagar

    ReplyDelete
  12. This seems to be a great article. I would have stuck in the middle if I encounter such problem. Now I've got an idea and I can use it when required. Thanks for sharing this informative post.
    Javascript Training in Chennai | Javascript Training Center in Chennai | Javascript Training Classes | Javascript Training

    ReplyDelete
  13. Very Good Blog. Highly valuable information have been shared. Highly useful blog..Great information has been shared. We expect many more blogs from the author. Special thanks for sharing..
    SAP Training in Chennai | AWS Training in Chennai | Android Training in Chennai | Selenium Training in Chennai | Networking Training in Chennai

    ReplyDelete
  14. WhatsApp Status Video Download :WhatsApp introduced the status feature in 2015, in which we can share images, videos, and gifs as our story for 24 hours. Before this feature, WhatsApp had only text status option in which we can write our bio, but the new status feature is different. The story or status disappears after 24 hours and can’t be archived as still in WhatsApp.

    Boy attitude status video download for whatsApp
    Boy attitude status video download
    Boy attitude status video download

    Most romantic status video download for whatsApp
    Sad video status download
    Most Romantic status video download

    video status download for whatsApp


    we have latest & best collection of video status download for whatsapp

    ReplyDelete
  15. I learned World's Trending Technology from certified experts for free of cost. I Got a job in decent Top

    MNC Company with handsome 14 LPA salary, I have learned the World's Trending Technology from Data science training in btm

    layout
    experts who know advanced concepts which can help to solve any type of Real-time issues in

    the field of Python. Really worth trying Freelance seo expert in

    Bangalore

    ReplyDelete
  16. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. Machine Learning Final Year Projects In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete
  17. My engagement ring is platinum and I couldn't be happier. It is so shiny however the downside is that it's pretty expensive and costs more to size than gold. There are scratches although not very noticeable seaport hack

    ReplyDelete
  18. They seem to be easy but infact requires a lot of time and hardwork . Great work world of tanks blitz hack

    ReplyDelete
  19. I am really happy with your blog because your article is very unique and powerful for new.
    Data Science
    Selenium
    ETL Testing
    AWS
    Python Online Classes

    ReplyDelete
  20. Very informative post.

    ReplyDelete
  21. This blog is very useful it include very knowledgeable information. Thankyou for sharing this blog with us. If anyone want to experience certificate in bangalore can call at 9599119376 or can visit https://experiencecertificates.com/experience-certificate-provider-in-bangalore.html

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete
  23. Great job for publishing such a nice article. Your article isn’t only useful but it is additionally really informative. Thank you because you have been willing to share information with us. Read more info about Job Oriented Vlsi Courses

    ReplyDelete
  24. This blog is very useful for me it gives me the very knowledgeable information to me. Dreamsoft is the 20years old consultancy providing the experience certificate in many status of the India. the interested may contact at the 9599119376 or can visit our website for the
    Career Boosting Genuine Experience Certificate In Mumbai
    https://experiencecertificates.com/experience-certificate-provider-in-mumbai.html
    Career Boosting Genuine Experience Certificate In Gurugram
    https://experiencecertificates.com/experience-certificate-provider-in-Gurgaon.html
    Career Bosting Genuine Experience Certificate In Delhi
    https://experiencecertificates.com/experience-certificate-provider-in-delhi.html
    Career Boosting Experience Certificate In Noida
    https://experiencecertificates.com/experience-certificate-provider-in-Noida.html
    Career Boosting Genuine Experience Certificate In Bangalore
    https://experiencecertificates.com/experience-certificate-provider-in-bangalore.html
    Career Boosting Genuine Experience Certificate Hyderabad
    https://experiencecertificates.com/experience-certificate-provider-in-Hyderabad.html

    ReplyDelete
  25. Laser printers print far more copies than an inkjet due to the use of a toner. Yes, a toner can certainly crank out a great many more copies than what the ink cartridges from an inkjet. Over time, a toner can prove to save a significant amount of money over time. renting impresoras barcelona

    ReplyDelete
  26. This article shares really good content. Very nice to read the article. Keep updating.
    Immigration Lawyer Near Me Virginia
    Personal Injury Lawyers Near Me Virginia

    ReplyDelete
  27. If you're in need of quality Suzuki Wagon-R Parts, you can't go wrong with our selection here at Suzuki Alto Spare Parts. We carry a wide range of parts for your car, so you can get back on the road in no time. Plus, our parts are all backed by a 100% satisfaction guarantee, so you can shop with confidence.

    ReplyDelete
  28. Wonderful work! keep sharing articles like this very insightful information and thank you for your great work towards this blog.
    software companies in chennai

    ReplyDelete