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