From c7f49b5e2a3bb5c24b2fa5b41a74ccfc6779b3ed Mon Sep 17 00:00:00 2001 From: Anna Novikova Date: Fri, 10 Nov 2023 13:36:20 +0100 Subject: [PATCH] EnumerateDuplicates --- src/SmartCommander/Assets/Resources.resx | 4 +- src/SmartCommander/Utils.cs | 45 ++++++++++++++++++- .../ViewModels/MainWindowViewModel.cs | 35 +++++++-------- 3 files changed, 61 insertions(+), 23 deletions(-) diff --git a/src/SmartCommander/Assets/Resources.resx b/src/SmartCommander/Assets/Resources.resx index 06b7f9f..3d03747 100644 --- a/src/SmartCommander/Assets/Resources.resx +++ b/src/SmartCommander/Assets/Resources.resx @@ -124,13 +124,13 @@ Cancel - Can't copy file to itself + Can't copy files to the same directory Can't edit the folder - Can't move file to itself + Can't move files to the same directory Can't move folder here diff --git a/src/SmartCommander/Utils.cs b/src/SmartCommander/Utils.cs index e005892..ca58916 100644 --- a/src/SmartCommander/Utils.cs +++ b/src/SmartCommander/Utils.cs @@ -39,7 +39,21 @@ static internal List GetDuplicates(List selectedItems, st { var duplicates = new List(); - // TODO: iterate through selectedItems + foreach (var item in selectedItems) + { + string targetFilePath = Path.Combine(destpath, item.Name); + if (item.IsFolder) + { + EnumerateDuplicates(item.FullName, targetFilePath, ref duplicates); + } + else + { + if (File.Exists(targetFilePath)) + { + duplicates.Add(item.FullName); + } + } + } return duplicates; } @@ -105,5 +119,34 @@ static internal void CopyDirectory(string sourceDir, string destinationDir, bool } } } + + static internal void EnumerateDuplicates(string sourceDir, string destinationDir, ref List duplicates) + { + // Get information about the source directory + var dir = new DirectoryInfo(sourceDir); + + // Check if the source directory exists + if (!dir.Exists) + throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}"); + + // Cache directories before we start copying + DirectoryInfo[] dirs = dir.GetDirectories(); + + // Get the files in the source directory and copy to the destination directory + foreach (FileInfo file in dir.GetFiles()) + { + string targetFilePath = Path.Combine(destinationDir, file.Name); + if (File.Exists(targetFilePath)) + { + duplicates.Add(file.FullName); + } + } + + foreach (DirectoryInfo subDir in dirs) + { + string newDestinationDir = Path.Combine(destinationDir, subDir.Name); + EnumerateDuplicates(subDir.FullName, newDestinationDir, ref duplicates); + } + } } } diff --git a/src/SmartCommander/ViewModels/MainWindowViewModel.cs b/src/SmartCommander/ViewModels/MainWindowViewModel.cs index fc38194..c6ac109 100644 --- a/src/SmartCommander/ViewModels/MainWindowViewModel.cs +++ b/src/SmartCommander/ViewModels/MainWindowViewModel.cs @@ -199,6 +199,11 @@ public async Task Copy() { if (SelectedPane.CurrentItems.Count < 1) return; + if (SelectedPane.CurrentDirectory == SecondPane.CurrentDirectory) + { + MessageBox_Show(null, Resources.CantCopyFileToItself, Resources.Alert); + return; + } var text = SelectedPane.CurrentItems.Count == 1 ? SelectedPane.CurrentItems[0].Name : string.Format(Resources.ItemsNumber, SelectedPane.CurrentItems.Count); var copy = new CopyMoveViewModel(true, text, SecondPane.CurrentDirectory); @@ -251,15 +256,8 @@ private void CopySelectedFiles(bool overwrite) { // copy file string destFile = Path.Combine(SecondPane.CurrentDirectory, Path.GetFileName(item.FullName)); - // TODO: move this check to the top level - if (destFile == item.FullName) - { - MessageBox_Show(null, Resources.CantCopyFileToItself, Resources.Alert); - } - else - { - File.Copy(item.FullName, destFile, overwrite); - } + File.Copy(item.FullName, destFile, overwrite); + } } SelectedPane.Update(); @@ -270,6 +268,11 @@ public async Task Move() { if (SelectedPane.CurrentItems.Count < 1) return; + if (SelectedPane.CurrentDirectory == SecondPane.CurrentDirectory) + { + MessageBox_Show(null, Resources.CantMoveFileToItself, Resources.Alert); + return; + } var text = SelectedPane.CurrentItems.Count == 1 ? SelectedPane.CurrentItems[0].Name : string.Format(Resources.ItemsNumber, SelectedPane.CurrentItems.Count); var copy = new CopyMoveViewModel(false, text, SecondPane.CurrentDirectory); @@ -283,7 +286,7 @@ public async Task Move() text = duplicates.Count == 1 ? Path.GetFileName(duplicates[0]) : string.Format(Resources.ItemsNumber, duplicates.Count); MessageBox_Show(MoveFileExists, string.Format(Resources.FileExistsRewrite, text), - Resources.Alert, ButtonEnum.YesNoCancel); + Resources.Alert, ButtonEnum.YesNoCancel); } else { @@ -328,16 +331,8 @@ private void MoveSelectedItems(bool overwrite) else { // move file - string destFile = Path.Combine(SecondPane.CurrentDirectory, Path.GetFileName(item.FullName)); - // TODO: move this check to the top level - if (destFile == item.FullName) - { - MessageBox_Show(null, Resources.CantMoveFileToItself, Resources.Alert); - } - else - { - File.Move(item.FullName, destFile, overwrite); - } + string destFile = Path.Combine(SecondPane.CurrentDirectory, Path.GetFileName(item.FullName)); + File.Move(item.FullName, destFile, overwrite); } } SelectedPane.Update();