Setting Remove to True in GetFiles removes the whole direct

Advertisement

andrewkc69
Joined:
Posts:
6
Location:
Portland

Setting Remove to True in GetFiles removes the whole direct

I am using the latest version of WinSCP, and I am using the .NET assemblies.

In the transferOptions, I set the FileMask. When I get files using the session.GetFiles, and set the Remove parameter to True, it works fine if there is only one file in the directory (and it removes the correct one). However, if there is only the one file in the directory, it removes the file AND the directory. How can I prevent it from removing the directory? The filemask in the code below is being set to ODS*.txt.

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

Reply with quote

Advertisement

andrewkc69
Joined:
Posts:
6
Location:
Portland

Re: Setting Remove to True in GetFiles removes the whole direct

martin wrote:


Martin,
This is not a duplicate post. At least it's not a duplicate to the one you linked to. This one is about setting the True flag in the GetFiles method and the other one is about using FileMask. Just because I used the same code in both topics, you are calling it a duplicate?? Did you read the issue, or am I missing how the FileMask and GetFiles issues are related?? I used the same code in both posts because they were separate issues. If I am missing something else here that makes them duplicates, please help me understand.

Thanks,
Andy

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,518
Location:
Prague, Czechia

Re: Setting Remove to True in GetFiles removes the whole direct

Sorry, I didn't notice the difference.

Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

Advertisement

You can post new topics in this forum