Skip to content

Commit 994f058

Browse files
Fix: Fixed an issue where the conflicts dialog didn't append numbers when pasting files (#15267)
1 parent 9ebd55b commit 994f058

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/Files.App/Utils/Storage/Operations/FileOperationsHelpers.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,10 @@ public static Task SetClipboard(string[] filesToCopy, DataPackageOperation opera
580580
{
581581
using ShellItem shi = new(fileToCopyPath[i]);
582582
using ShellFolder shd = new(Path.GetDirectoryName(copyDestination[i]));
583-
584-
// Performa copy operation
585-
op.QueueCopyOperation(shi, shd, Path.GetFileName(copyDestination[i]));
583+
584+
var fileName = GetIncrementalName(overwriteOnCopy, copyDestination[i], fileToCopyPath[i]);
585+
// Perform a copy operation
586+
op.QueueCopyOperation(shi, shd, fileName);
586587
}))
587588
{
588589
shellOperationResult.Items.Add(new ShellOperationItemResult()
@@ -1018,5 +1019,26 @@ protected override void Dispose(bool disposing)
10181019
}
10191020
}
10201021
}
1022+
1023+
private static string GetIncrementalName(bool overWriteOnCopy, string? filePathToCheck, string? filePathToCopy)
1024+
{
1025+
if (filePathToCheck == null)
1026+
return null;
1027+
1028+
if ((!Path.Exists(filePathToCheck)) || overWriteOnCopy || filePathToCheck == filePathToCopy)
1029+
return Path.GetFileName(filePathToCheck);
1030+
1031+
var index = 2;
1032+
var filePath = filePathToCheck;
1033+
if (Path.HasExtension(filePathToCheck))
1034+
filePath = filePathToCheck.Substring(0, filePathToCheck.LastIndexOf("."));
1035+
1036+
Func<int, string> genFilePath = x => string.Concat([filePath, " (", x.ToString(), ")", Path.GetExtension(filePathToCheck)]);
1037+
1038+
while (Path.Exists(genFilePath(index)))
1039+
index++;
1040+
1041+
return Path.GetFileName(genFilePath(index));
1042+
}
10211043
}
10221044
}

src/Files.App/Utils/Storage/Operations/FilesystemHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ public async Task<ReturnResult> CopyItemsAsync(IEnumerable<IStorageItemWithPath>
325325
{
326326
foreach (var item2 in itemsResult)
327327
{
328-
if (!string.IsNullOrEmpty(item2.CustomName) && item2.SourcePath == item.Key.Path)
328+
if (!string.IsNullOrEmpty(item2.CustomName) && item2.SourcePath == item.Key.Path && Path.GetFileName(item2.SourcePath) != item2.CustomName)
329329
{
330330
var renameHistory = await filesystemOperations.RenameAsync(item.Value, item2.CustomName, NameCollisionOption.FailIfExists, banner.ProgressEventSource, token);
331331
history.Destination[history.Source.IndexOf(item.Key)] = renameHistory.Destination[0];

0 commit comments

Comments
 (0)