Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

andrewkc69

Re: FileExists Does not work with FileMask

martin 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.
andrewkc69

FileExists Does not work with FileMask

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?
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());
        }
    }
}