PiousVenom
PiousVenom

Reputation: 6908

Check if list of files exist on FTP server

I'm looking for a way, if possible, to connect to an ftp server, and then iterate through a list of local files to check their existence on the server. Now, I've found:

var request = (FtpWebRequest)WebRequest.Create("ftp://ftp.domain.com/doesntexist.txt");

request.Credentials = new NetworkCredential("user", "pass");
request.Method = WebRequestMethods.Ftp.GetFileSize;

try
{
    var response = (FtpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
    var response = (FtpWebResponse)ex.Response;
    if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
    {
        //Does not exist
    }
}

But, if I read this right (I haven't had my coffee yet), it creates a WebRequest for each file. The overall goal is to check if these files exist, and if not, upload them. My question is, do I HAVE to do this individually, or is it possible(or even feasible) to just connect to the FTP once and then do my check/uploads?

Upvotes: 0

Views: 2055

Answers (1)

VladL
VladL

Reputation: 13033

1) Get a list of files from FTP as described here

2) Get a list of local files using Directory.GetFiles()

3) loop through one list and check if the other list contains the item

Upvotes: 2

Related Questions