Topic "FileExists Does not work with FileMask"

Author Message
andrewkc69
[View user's profile]

Joined: 2015-03-27
Posts: 6
Location: Portland
I am using the latest version of WinSCP, and I am using the .NET assemblies.

In my code (pasted below), I set the transferoptions filemask. Based on the documentation, it doesn't look like session.FileExists supports this, so I included it. My filemask is ODS*.txt.

When I run the code with the filemask in the session.FileExists, it returns false, even if the file exists. On the other hand, if I don't include the filemask (and just use the directory) it returns true if there are any files in the directory. When it runs the session.GetFiles, the transferResult returns true for transferResult.IsSuccess, even though no files were transferred. I can I check to see if a file exists in the directory, using a filemask?

Code:

 SessionOptions sessionOptions = new SessionOptions();


                    if (job.ConnectionType == "sftp")
                    {
                        sessionOptions.Protocol = Protocol.Sftp;
                        sessionOptions.GiveUpSecurityAndAcceptAnySshHostKey = true;
                    }

                    sessionOptions.HostName = job.FTPAddress;
                    sessionOptions.PortNumber = Convert.ToInt32(job.port);
                    sessionOptions.UserName = job.Username;
                    sessionOptions.Password = job.Password;

                    using (Session session = new Session())
                    {
                        // Connect
                        session.Open(sessionOptions);

                        // Upload files
                        TransferOptions transferOptions = new TransferOptions();
                        transferOptions.TransferMode = TransferMode.Binary;
                        transferOptions.FileMask = job.PickUpDirectoryMask;

                        TransferOperationResult transferResult = null;


                        if (job.FTPType == "get")
                        {
                            if (session.FileExists(job.PickUpDirectory))
                            {

                                transferResult = session.GetFiles(job.PickUpDirectory, job.DropOffDirectory, true, transferOptions);

                            }
                            else
                            {
                                LogFTPStatus(job.JobId, false, "Scheduled", "Fail - File does not exist.", log);
                            }

                        }
                        else
                        {
                            if (session.FileExists(job.PickUpDirectory))
                            {
                                transferResult = session.PutFiles(job.PickUpDirectory, job.DropOffDirectory, true, transferOptions);
                            }
                            else
                            {
                                LogFTPStatus(job.JobId, false, "Scheduled", "Fail - File does not exist.", log);
                            }
                        }


                        if (transferResult != null)
                        {
                            if (transferResult.IsSuccess)
                            {
                                //The transfer can still look like a success even if no files were transfered. Not sure why
                                if (transferResult.Transfers.Count >= 1)
                                {
                                    LogFTPStatus(job.JobId, true, "Scheduled", "Success", log);
                                }
                                else if(transferResult.Transfers.Count == 0)
                                {
                                    LogFTPStatus(job.JobId, false, "Scheduled", "Fail - File does not exist.", log);
                                }
                            }
                            else
                            {
                                transferResult.Check();
                                LogFTPStatus(job.JobId, false, "Scheduled", "Fail", log);
                                log.WriteEntry("Failures were: " + transferResult.Failures.ToString());
                            }
                        }
                       
                    }
                }
                else
                {

                    log.WriteEntry("No transfers to run");
                }
            }
            catch (Exception e)
            {
                //Write the exception to the event log
                log.WriteEntry("Error in FTP Transfer: " + e);

                //Write the exception to the system log
                LogFTPStatus(job.JobId, false, "Scheduled", e.InnerException.ToString(), log);
            }
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24991
Location: Prague, Czechia
Calling FileExists with a path to a directory tests, if the directory exists, not if files exist in the directory.

See https://winscp.net/eng/docs/script_checking_file_existence
_________________
Martin Prikryl
andrewkc69
[View user's profile]

Joined: 2015-03-27
Posts: 6
Location: Portland
prikryl wrote:
Calling FileExists with a path to a directory tests, if the directory exists, not if files exist in the directory.

See https://winscp.net/eng/docs/script_checking_file_existence


Let me make sure I understand you correctly. A method called "FileExists", which returns a bool, only checks to see if the directory exists? Then why is the method called "FileExists"? Do I need to use a method called "DirectoryExists" to see if a file inside a directory exists? I'm sorry, but that just seems like poor design to create a method that looks like it's supposed to see if a files exists, when it actually looks at the directory.
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License