3

I need to copy the following subset of folders from $sourceDir to $targetDir:

abc0001
abc0643
abc0456
...

The number of folders is unknown, but they all match a pattern abc0*.

Is there an elegant solution to expand abc0* to the actual list of folders and then copy them? I tried this:

dir "$sourceDir\abc0*" -Recurse | copy -Destination $targetDir -WhatIf

But it does not preserve the path, so all files end up in the root of $targetDir.

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151

1 Answers1

3

Give this a try:

dir $sourceDir abc0* | where {$_.psiscontainer} | copy -dest $targetDir -recurse
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • why not adding the `-recurse` to `get-childitem` also? – CB. Jun 14 '13 at 20:46
  • I guess you could do that too but it will be quicker, recursing with copy will drill down on incoming folders only while with gci it will recurse on all child items. – Shay Levy Jun 14 '13 at 20:52