1

I am trying to copy folder and its sub folder which has some files in it. For example my folder1 contains folders like abc , xyx and again inside this folder I have files. I want to copy the full folder such as folder1 and paste it in the destination, if it exists then replace all the files in it. I am using the below commands.

My problem is if I run this script multiple times it is creating subfolders inside subfolder, like inside abc it is again creating abc which I do not want.

$sourceFolder='C:\abc\foder1'
$destFolder='c:\Test\folder1'

Get-ChildItem $sourceFolder | ForEach-Object `
{
    $destFullPath = Join-Path $destFolder ($_.BaseName + $_.Extension)
    Copy-Item -Path $_.FullName -Destination $destFullPath  -Force  -Recurse 
}  
Vivek Kumar Singh
  • 3,223
  • 1
  • 14
  • 27
Ashis
  • 45
  • 1
  • 4
  • Your text says `folder1` contains `abc,xyz`, in your code it's vice versa? –  Dec 13 '18 at 10:58
  • could you please help me with code .. I did not get how to get this done. I know something is creating multiple folder and files if I run it many times. – Ashis Dec 13 '18 at 11:02
  • This is a known issue of Copy-Item. See here for details https://superuser.com/questions/544520/how-can-i-copy-a-directory-overwriting-its-contents-if-it-exists-using-powershe – montonero Dec 13 '18 at 11:11
  • @Ashis: The linked question is essentially the same and has an answer that should work for you. – mklement0 Dec 13 '18 at 19:57

0 Answers0