markwragg / PowerShell-HashCopy

A PowerShell module for copying files between two paths that have been determined to have changed via computed hash value comparison.
44 stars 8 forks source link

-Mirror parameter doesn't work properly #7

Open nebffa opened 4 years ago

nebffa commented 4 years ago

Hey, this module is really awesome! However, the -Mirror parameter doesn't work correctly for me - it deletes all of the files I want to copy across for some reason. A way to reproduce it is:

  1. Copy a directory without the -Mirror switch
  2. Copy the same directory with the -Mirror and the -WhatIf switches - it will tell you that it's going to delete all the files

EDIT: After a bit more checking: Not working: Copy-FileHash -Path C:\somePath -Destination C:\someOtherPath Working: Copy-FileHash -Path C:\somePath\ -Destination C:\someOtherPath\

markwragg commented 4 years ago

Sorry for taking so long to come back to you on this. I've not been able to reproduce the issue you describe on either MacOS or Windows. Can you let me know what version of the module you were using? (Get-Module HashCopy -ListAvailable should show you.)

Kreloc commented 3 years ago

Hi, I have been using your module and I am also experiencing this issue as well.

The folder given to -Path needs to have subfolders within it that contain files. In my case, there are version folders in root of the source i.e. "C:\Scripts\Module\SomeModule\3.0.0" that then contain files as well as more subdirectories that contain files.

Below are some details about what I found looking into what was occurring. The line I was running:

Copy-FileHash -Path "C:\Scripts\Module\SomeModule" -Destination "C:\That\Is\DestinationStuff" -Recurse -Mirror

This was causing all of the files copied over to "C:\That\Is\DestinationStuff" and then files to be deleted that were not in the root of the folder.

This was due to the path coming back during the formation of $SourceFile as malformed:

            ForEach ($DestFile in $DestFiles) {
                $SourceFile = Get-DestinationFilePath -File $DestFile -Source $Destination -Destination $Source

Where $SourceFile was coming back as C:\Scripts\Module\SomeModule3.0.0\SomeModule.psd1 when it should have come back as C:\Scripts\Module\SomeModule\3.0.0\SomeModule.psd1

Which the code is then doing Test-Path for each of those, rightfully coming back as $False, and doing the Remove-Item operation on each $DestFile not within the root directory.

Changing line 110 to no longer append the Folder separator fixes this behavior (not sure what the intent was of doing that Join-Path there with '/' ):

$Destination = (Resolve-Path -Path $Destination).Path

I thought I'd add my experience with this particular issue.

Thank you for making this module!

gvdm90 commented 3 years ago

Hi! Awesome module, thank you! I've experienced this same issue with subfolders being cleared using the -Mirror flag. I did some debug and fixed the Get-DestinationFilePath function this way:

Buggy:

    $DestFile = $DestFile -Replace "^$([Regex]::Escape((Convert-Path $Source)))", $Destination

Fixed:

    $DestFile = $DestFile -Replace "^$([Regex]::Escape((Convert-Path $Source)))", $($Destination + "\")

Is there is any way to contribute I could perform this fix on the master branch

Buena-Vista commented 3 years ago

I, too, was in great need of a module like this and it works great for new or modified files. Thanks! It would be even better if the -Mirror worked correctly for orphaned files. I'll see if I can figure out how to change the module that I downloaded using gvdm90's comment, but I have zero experience with powershell, so I might make things worse. It would be great if someone could fix the master file. Thanks again!

edit: well, that was easier than I expected and now the script does the trick! I did discover another bit of strangeness, though. I have one rap track with left and right brackets in it [explicit] and that got deleted and also added. I don't buy much of that, so it's not a big problem for me. Thanks to both of you.