2

I want to copy a folder, complete with subdirectories, files and files within subdirectories, preserving the structure and create them in a new location that did not previously exist. This is my PowerShell code

Copy-Item c:\development\powershell\folderone\* c:\development\powershell\foldertwo -recurse -Container 
Copy-Item c:\development\powershell\folderone\* c:\development\powershell\folderthree -recurse -Container

foldertwo exists and is empty, folderthree does not exist.

If I run the script, the structure is created correctly in foldertwo, however folderthree gets created, but contains only all the files from the entire substructure, all at the root folderthree level. It has not recreated the subfolders within folderone, just put all the files at the root level of folderthree.

What have I done wrong?

Mofi
  • 46,139
  • 17
  • 80
  • 143
NZJames
  • 4,963
  • 15
  • 50
  • 100
  • 1
    If I understand the issue correctly, you may want to look at using `Test-Path` first, and then, if it returns as `false`, create the directory, ala [this](http://stackoverflow.com/a/16911470/2486496) answer here. – gravity Jul 13 '16 at 15:31
  • 2
    Thanks, that did the trick – NZJames Jul 13 '16 at 15:35
  • I can't reproduce that behaviour, if I use `copy-item c:\test c:\test2 -recurse -container` (with no `*`) then it works and puts files in subfolders. If I try it with a `*` in the source as you have it, I get errors and it doesn't copy everything at all - but it doesn't dump things in the root. Did you try it without the asterisk in the source? – TessellatingHeckler Jul 13 '16 at 19:49

2 Answers2

2

Here's a very basic, but fully working and tested example, building on your confirmation above that I understood the issue at-hand:

$folderlist = ("foldertwo", "folderthree")
foreach ($folder in $folderlist)
{
    if (!(Test-Path "C:\Development\PowerShell\$folder"))
    {
        mkdir ("C:\Development\PowerShell\$folder") | Out-Null
    }
    Copy-Item c:\development\powershell\folderone\* c:\development\powershell\$folder -recurse -Container
}
gravity
  • 2,175
  • 2
  • 26
  • 34
  • Thanks for the help here... I have one file outside my foldernames, so how can I copy it once? – RadFox Apr 10 '17 at 21:48
  • This would be a good opportunity to post a new question yourself, and earn rep properly, @Underdog. Consider reading [ask] and link to this Q&A to expand your question easier. :-) – gravity Apr 11 '17 at 17:33
2

From what I understand, the question is about recreating the folder structure from [source] to [destination]. As using CmdLets is kind of overkill (and performance loss), I suggest simple batch command that may also be ran in powershell.

xcopy [source] [destination] /T /E

xcopy is a function to copy file and directory trees. Help provides us info on usefult parameters on the case:

/T Creates directory structure, but does not copy files. Does not include empty directories or subdirectories. /T /E includes

/E Copies directories and subdirectories, including empty ones. Same as /S /E. May be used to modify /T.

Deptor
  • 333
  • 3
  • 9