Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-Mirror parameter doesn't work properly #7

Open
nebffa opened this issue Aug 11, 2020 · 4 comments
Open

-Mirror parameter doesn't work properly #7

nebffa opened this issue Aug 11, 2020 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@nebffa
Copy link

nebffa commented Aug 11, 2020

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 markwragg added the bug Something isn't working label Aug 11, 2020
@markwragg
Copy link
Owner

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
Copy link

Kreloc commented Feb 24, 2021

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
Copy link

gvdm90 commented Jun 9, 2021

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
Copy link

Buena-Vista commented Jun 21, 2021

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.

@markwragg markwragg self-assigned this Sep 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants