SSH.NET icon indicating copy to clipboard operation
SSH.NET copied to clipboard

MoveTo does not work as expected

Open fijsh opened this issue 5 months ago • 3 comments

I'm trying to move file abc.txt to a different folder and file name using the following code:

var files = client.ListDirectory("MyFiles/In").ToList();

foreach (var file in files.Where(f => f.IsRegularFile)) {
	var newFilePath = $"MyFiles/In/archive/{DateTime.Now:yyyyMMddHHmmss}-{file.Name}";
	file.MoveTo(newFilePath);
}

When the source file name abc.txt already exists in the archive sub folder the library throws an exception with message Already exists even though the target file name will have a different name (prefixed with a date time stamp).

fijsh avatar Jul 29 '25 13:07 fijsh

We have tests that validate MoveTo e.g. https://github.com/sshnet/SSH.NET/blob/03ae6bfa0f1bda7635d3a4eb8888bebf3fab1105/test/Renci.SshNet.IntegrationTests/OldIntegrationTests/SftpFileTest.cs#L162 which seem to behave as expected (even with some extra checks).

What are the full error details? Does it happen every time? And silly question: are you sure that newFilePath does not already exist?

Rob-Hague avatar Aug 03 '25 15:08 Rob-Hague

I found a related issue #756

Rob-Hague avatar Aug 03 '25 18:08 Rob-Hague

Not a silly question, but I triple checked that the file did not already exist. The full message was "Already exists". I tried to reproduce it again today, but strangely enough now it works correctly. Perhaps some locking issues? I will do some more tests today.

fijsh avatar Aug 04 '25 09:08 fijsh