Skip to content

Commit

Permalink
EnumerateDuplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
anovik committed Nov 10, 2023
1 parent 946379c commit c7f49b5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/SmartCommander/Assets/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@
<value>Cancel</value>
</data>
<data name="CantCopyFileToItself" xml:space="preserve">
<value>Can't copy file to itself</value>
<value>Can't copy files to the same directory</value>
</data>
<data name="CantEditFolder" xml:space="preserve">
<value>Can't edit the folder</value>
</data>
<data name="CantMoveFileToItself" xml:space="preserve">
<value>Can't move file to itself</value>
<value>Can't move files to the same directory</value>
</data>
<data name="CantMoveFolderHere" xml:space="preserve">
<value>Can't move folder here</value>
Expand Down
45 changes: 44 additions & 1 deletion src/SmartCommander/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,21 @@ static internal List<string> GetDuplicates(List<FileViewModel> selectedItems, st
{
var duplicates = new List<string>();

// 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;
}
Expand Down Expand Up @@ -105,5 +119,34 @@ static internal void CopyDirectory(string sourceDir, string destinationDir, bool
}
}
}

static internal void EnumerateDuplicates(string sourceDir, string destinationDir, ref List<string> 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);
}
}
}
}
35 changes: 15 additions & 20 deletions src/SmartCommander/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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
{
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit c7f49b5

Please sign in to comment.