0

I need to know whether a particular file exists on a given FTP server when I run a job that calls the said SSIS package. Currently I'm employing a script task(within SSIS) that subsumes the following code:

        string userName = Dts.Variables["User::ftp_username"].Value.ToString();
        string password = Dts.Variables["User::ftp_password"].Value.ToString();
        string fileName = Dts.Variables["User::download_file_name"].Value.ToString();
        string ftpURL = String.Format("ftp://abc.def.com/{0}", fileName);

        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::fileExists"].Value = true;
            }
        }
        catch
        {
            Dts.Variables["User::fileExists"].Value = false;
        }

        Dts.TaskResult = (int)ScriptResults.Success;

However, this is calling the WebRequestMethods.Ftp.DownloadFile method, which according to the MSDN website is used to is used to download a file from an FTP server. I just need to know if the file exists, not download it.

Also, is there a better way to check for file existence?

PS. I'm new to C#, so I'm sorry if sound naive!

Ritesh Bhakre
  • 203
  • 3
  • 10

2 Answers2

0

I would remove that script altogether and use the FTP Task built into SSIS.

Send a small file, with overwrite set to false and process the error accordingly if it already exists - i.e. the FTP failed.

If the FTP was successful (i.e. the file did not exist!) - don't forget to delete it!

BIDeveloper
  • 2,628
  • 4
  • 36
  • 51
0

I would use listdirectory instead of downloadfile. http://www.niteshluharuka.com/how-to-list-all-files-directories-from-a-ftp-server-in-csharp/

Joe C
  • 3,925
  • 2
  • 11
  • 31