1

I have folder name XYZ into ftp server [ftp://ftp00.abcd.net] . I have some xml files into XYZ folder like Lggggg.XML .I want to download multiple files with same name like Lgggg.XML. Now I can download only file that's why I want to know count of files [Lgggg.XML] then I will run download file code within for loop. I want to count those xml files which exists into ftp://ftp00.abcd.net/XYZ . I searched through net but I didn't find proper solution . Any information regarding this will be helpful . Please help .

EDIT My C# code

      FtpWebRequest ftpwebrequest = (FtpWebRequest)WebRequest.Create("ftp://ftp00.abcd.net/XYZ"); 
      ftpwebrequest.Credentials = new NetworkCredential("username", "pwd");
      ftpwebrequest.Method = WebRequestMethods.Ftp.ListDirectory;
      FtpWebResponse response = (FtpWebResponse)ftpwebrequest.GetResponse();

      Console.WriteLine("user looged in");

      Stream responseStream = response.GetResponseStream();
      StreamReader reader = new StreamReader(responseStream);
      Console.WriteLine(reader.ReadToEnd());

      Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);
      var list = Directory.GetFiles("vit-gent-maid", "*.XML");  
     int count = list.Length;

     Console.WriteLine("count :" + count);
     using (WebClient ftpClient = new WebClient())
     {
        ftpClient.Credentials = new System.Net.NetworkCredential("username", "pwd");
        ftpClient.DownloadFile("ftp://ftp00.abcd.net/XYZ/Lggggg.XML", @"C:\\Temp\XML\Lggggg.XML");
        Console.WriteLine("file transfer successful");
     }
        Console.ReadLine();

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            Console.ReadLine();

        }
ZMAX
  • 149
  • 1
  • 12
  • You will have to issue the `LS` or `DIR` command to get a listing of the files. Capture that output and increment a counter. – Squashman Mar 14 '18 at 23:07
  • @Squashman thanks for reply . I want C# code to count files into ftp folder – ZMAX Mar 14 '18 at 23:10
  • https://stackoverflow.com/questions/3298922/how-to-list-directory-contents-with-ftp-in-c – ragerory Mar 14 '18 at 23:11
  • 2
    Would be helpful to provide code you tried, rather than just asking for code. This is a fairly simple problem that has been solved many times and there are examples all over the web. – ragerory Mar 14 '18 at 23:12
  • 1
    Possible duplicate of [How to List Directory Contents with FTP in C#?](https://stackoverflow.com/questions/3298922/how-to-list-directory-contents-with-ftp-in-c) – ragerory Mar 14 '18 at 23:18
  • my question is not same as how to list directory contents. there are some xml files into a ftp directory. I want to count the files i.e how many files are present into that directory . i searched stackoverflow and didnt find solution that's why i'm asking – ZMAX Mar 14 '18 at 23:29
  • @SudiptaSaha if you're looking for the count of files, you can get that directly from the list of the Directory's contents. What part are you missing? You're also not telling us where your error is or what you're seeing. You just posted some code and said it doesn't work. Be specific. We're here to help, not do your work for you. – ragerory Mar 14 '18 at 23:31
  • The results from ftp directory is a string with '\n' at end of each line. So you can put results into a StringReader() and then read one line at a time to process results. StringReader files = new StringReader(response.text) – jdweng Mar 14 '18 at 23:42
  • @SudiptaSaha What do you mean by "it doesn't work"? What error are you receiving? What are the contents of those variables? What does the contents of your directory look like? – ragerory Mar 15 '18 at 13:23

0 Answers0