0

I want to copy folder 1 from the source directory to the destination directory however folder 1 is always a randomly generated number.

Source/Folder A:  
    folder 1 (random number)  
        file1.txt  
        file2.txt  

Destination/Folder A:  
    folder 1 (random number)  
        file1.txt  
        file2.txt  
        thisfile.txt  

Because it is randomly generated there is no way for me to go to the destination directory before and delete the contents of the folder.

I have tried to do this to compare and delete all of the items from the destination folder that are not in the source folder before I copy it however it seems to delete all of the files in the destination folder.

$A = "C:\source\folderA"
$B = "C:\destination\folderA"

Get-ChildItem $B -Recurse -File |Where-Object {! (Test-Path ("$A\$($_.Name)"))} |Remove-Item -force
Cpt.Whale
  • 4,784
  • 1
  • 10
  • 16
HelpMePlz
  • 51
  • 7
  • 1
    Not sure what you're trying to accomplish here. Do you want to delete files in the destination that don't exist in the source? Are you just trying to copy and overwrite from source to destination (which would just be adding `-force` to your `copy-item` command, no need to pre-delete things)? Please specify your desired outcome. – TheMadTechnician Jan 26 '23 at 19:10
  • Yes I would like to delete files in the destination that dont exist in the source – HelpMePlz Jan 26 '23 at 19:11
  • why not delete everything in the destination and copy the source over after? then it's all the same. Or just use robocopy with the /mir parameter. – TheMadTechnician Jan 26 '23 at 19:24
  • 1
    @HelpMePlz It may be easier to `Rename-Object` the destination "folder 1" to the source's random number before copying if that's the issue. `robocopy /MIR` is definitely best for purging stuff that's not necessary, but it does need the structure to match first – Cpt.Whale Jan 26 '23 at 22:52

0 Answers0