locked
copy-item not overwriting exising files but creating additional subfolder. RRS feed

  • Question

  • Hello, A simple bit of code does not do as expected:

    copy-item "C:\folder1\*" -destination "\\server1\share1\folder2" -recurse

    I would expect this to copy all files from c:\folder 1 to the destination folder. If the destination folder does not exist it should create it however it doesn't. I get the error "Copy-Item : Could not find a part of the path".

    if I change the code to the following i also get unexpected results:

    copy-item "C:\folder1" -destination "\\server1\share1\folder2" -recurse

    If i run this once, the folder structure is created and the files are copied. If i run this again it created an additional folder in the tree and copies the files to that folder again so we end up with the following:

    \\server1\share1\folder2\folder2

    What am I doing wrong? All i want to do is copy the contents of a folder to another location and create it if ti does not exist.

    Thanks.

    Thursday, May 17, 2012 3:22 PM

Answers

  • Thanks for the reply. To get around the problem I have user a remove-item statement however I want ed to check the command for my own sanity as I was pretty sure it should have worked as I previously said. I can only assume I have a bug which causes this!
    • Marked as answer by Yan Li_ Friday, May 25, 2012 12:53 AM
    Friday, May 18, 2012 9:47 AM

All replies

  • The second command you tried should work, what version of PowerShell and Windows are you using? You could try using the -path Parameter in your command, but it is already implied so it should not make a difference. Here is the complete command:

    copy-item -path "C:\folder1" -destination "\\server1\share1\folder2" -recurse

    If it fails can you post exactly what command you are using so I can see where it is going wrong?


    Jaap Brasser
    http://www.jaapbrasser.com

    Thursday, May 17, 2012 3:52 PM
  • Thanks for the reply.

    I'm running this command on a Windows 7 PC using powershell 2. Effectively I am using this command to sync some files i have across a few servers but when I run the script for a second time it creates the second subfolder as shown below:

    \\server1\share1\folder2\folder2

    What's odd is that if I run the script for a 3rd time then I do not get a third subfolder created. Instead I get an error - Copy-Item : Item with specified name already exists.

    I'm not sure it makes any difference but i am using the C$ admin share rather than a manually shared folder.

    Heres the command I have used (Server names removed for security)

    copy-item -path "C:\Shared\Scripts\Powershell Scripts\Script_Update" -destination "\\servername\d$\software\Powershell_Scripts\Script_Update" -recurse


    • Edited by Aeropars Friday, May 18, 2012 8:42 AM
    Friday, May 18, 2012 8:41 AM
  • I have tried the exact command you are using without a problem, how about if you call robocopy from PowerShell by using this command:

    cmd /c robocopy.exe "C:\Shared\Scripts\Powershell Scripts\Script_Update" "\\servername\d$\software\Powershell_Scripts\Script_Update" /e /np /r:1 /w:1

    The Administrative share you are using is no problem, as long as you have to permissions to use that share it is as good as any share. 


    Jaap Brasser
    http://www.jaapbrasser.com

    Friday, May 18, 2012 9:14 AM
  • Thanks for the reply. To get around the problem I have user a remove-item statement however I want ed to check the command for my own sanity as I was pretty sure it should have worked as I previously said. I can only assume I have a bug which causes this!
    • Marked as answer by Yan Li_ Friday, May 25, 2012 12:53 AM
    Friday, May 18, 2012 9:47 AM
  • Copy-Item is confusing because, when copying a folder, if the destination doesn't exist it assumes you're trying to copy/rename the source to be the destination, whereas if the destination exists it assumes you're trying to copy underneath it.

    This is really annoying behavior and you can vote to change it by up-voting the issue here.

    In your second example, folder2 doesn't initially exist so it thinks you're trying to copy and rename folder1 to be "\\server1\share1\folder2". On subsequent runs, folder2 exists and it thinks you're trying to copy folder1 underneath it ("\\server1\share1\folder2\folder1").

    If you run it a third time, there's already a folder1 in the destination so you get "Item with specified name already exists" if you don't use the -Force flag to overwrite.

    The only other answer I found online is "use RoboCopy instead".  I didn't want to so I copied into the parent folder instead ("\\server1\share1\", in your example).  This gets ugly when paths aren't literal strings. For example, I had to use (get-item "c:\$_").parent.fullname as my destination.


    • Edited by JVimes Tuesday, February 11, 2014 10:00 PM accuracy
    Tuesday, February 11, 2014 9:54 PM