0

I am trying to FTP multiple files from server (particular directory say /x/) to local directory. I know the names of files which i want to FTP, Is there a way to FTP all the files residing in the particular directory of server in one command like "mget()" command in unix.

Currently i am doing following thing

using (WebClient ftpClient = new WebClient())
{
ftpClient.Credentials = // credentials

ftpClient.DownloadFile("ftp://<ftp ip>//x//y//" + "x0.txt", @"D:\\test\\x\\" + "check" + "_a0.txt");
ftpClient.DownloadFile("ftp://<ftp ip>//x//y//" + "x1.txt", @"D:\\test\\x\\" + "check" + "_a1.txt");
ftpClient.DownloadFile("ftp://<ftp ip>//x//y//" + "x2.txt", @"D:\\test\\x\\" + "check" + "_a2.txt");
}

This makes FTP connection each time it wants to download/ make copy of file to local directory.

Even If i read the directory contents by following command

reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
response = reqFTP.GetResponse();
                reader = new StreamReader(response.GetResponseStream());
                string line = reader.ReadLine();
                while (line != null)
                {
                    if (line.Contains("xyz"))
                    {
                        result.Append(line);
                        result.Append("\n");

                    }
                    line = reader.ReadLine();
                }

and then download each file individually, it still makes call for each file to FTP to local directory. I am looking for something which can match the pattern like "wildcard" and download all files in one command that matches the pattern or wildcard.

Thanks in advance for all the help.

Thanks

codeZach
  • 39
  • 5
  • I need the solution in C# – codeZach Jun 04 '18 at 23:40
  • You might be better off using a bespoke FTP library. NuGet should holds plenty of them. – Krumelur Jun 04 '18 at 23:48
  • In unix I used to use the wildcard *. – jdweng Jun 05 '18 at 04:26
  • Yes, I need something same like wildcard and it should download in one connection attempt. It's marked duplicate, but the flow which they have used is not what I am seeking, I have already implemented that too. Thanks for any help in advance – codeZach Jun 05 '18 at 15:12
  • 1
    @codeZach Can you explain why do you want to use a single call to download all files? 1) Do you want to use a single connection only? That code uses a single connection only. See https://stackoverflow.com/q/49012015/850848#49088223 2) Or do you want your code to be shorter? Then you have to use a 3rd party library, there's no shorter solution using pure .NET framework. For an example, see the solution using WinSCP .NET assembly at the end of [my/accepted answer to the duplicate question](https://stackoverflow.com/q/37038676/850848#37043702). 3) If you have another reason, please explain. – Martin Prikryl Jun 06 '18 at 07:35
  • @MartinPrikryl thanks for valuable input. As per my understanding/ or what i might have assumed is, in above code It make FTP connection everytime it downloads a file form the list of directory. If i am wrong, can you please explain why that is a single connection ? – codeZach Jun 06 '18 at 16:08
  • 1
    That's explained in my answer to another question, I've already linked in my previous comment! – Martin Prikryl Jun 06 '18 at 16:11
  • @MartinPrikryl thanks a lot for clearing my doubt. it helped a lot – codeZach Jun 06 '18 at 18:04

0 Answers0