Link to home
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

C# WinSCP code errors on multiple downloads

I have this C# code that uses WinSCP to download files. Today I had to point it to a set of files with a different prefix. In this case there could be more than one match on the SFTP server. This code correctly downloads the first matching file but then exits via the catch with the message "No such file" when it continues onto the next match. Commenting out the download code stops that message. Is there something I need to add to allow for this code to download multiple files?

------------------------------------------------------------------------------------------


                foreach (var entry in dirContents)
                {
                    // Check to see if the base filename of the remote file contains "hm_pos_"
                    if (entry.Name.Contains("hm_pos_"))
                    {
                        Console.WriteLine(" Filename : " + entry.Name);
                        dateOnly = entry.Attributes.LastWriteTime.Date;

                        // Search for PO files created today
                        if (entry.Attributes.LastWriteTime.Date == dateToday)
                        {
                            FilenameToSave = entry.FullName;
                            FilenameToSave = FilenameToSave.Replace("/upload/", "");        /* Remove the folder prefix from the filename */
                        }
                        if (FilenameToSave.Length > 0)
                        {
                            // Using the most recent filename, create a new local file with FileStream, and then download the remote file data into the local file
                            using (FileStream localFile = new FileStream(FilenameToSave, FileMode.Create))
                            {

                                sftpClient.DownloadFile(FilenameToSave, localFile);
                            }
                        }
                    }
                }

ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial