-2

I have to copy only folders from one location to another location on file system excluding the files inside the folders.

The below snippet i have tried is copies folders and files Ex:

MoveToPath\A\B\X.xml. Copy-Item $MoveFromPath $MoveToPath -recurse -EA SilentlyContinue

I want only "MoveToPath\A\B"

mak
  • 11
  • 3
  • Try googling Get-Childitem piping to a where clause, you'll probably want to use that and pipe it into a where clause that filters out files and create the folder at the destination. – CalebB Apr 20 '15 at 20:11
  • Get-ChildItem c:\temp -Recurse | ?{ $_.PSIsContainer } |copy-item -destination c:\target will copy directories, but doesn't maintain the directory structure. I.e., c:\temp\test\sub1 will end up as c:\target\sub1. – Tony Hinkle Apr 20 '15 at 21:21
  • Also, you could just call robocopy: robocopy c:\temp c:\target /e /xf * – Tony Hinkle Apr 20 '15 at 21:33

1 Answers1

0

This is keep the structure but you have to have permission (obviously) to the destination folder to create objects.

Clear-Host
$Source = "C:\SourceFolder"
$Destination = "D:\DestinationFolder"

Get-ChildItem $Source* -Recurse | ForEach-Object{
    if($_.Attributes -eq 'Directory')
    {
        # Build new files path with source folder name
        [string] $dirName = "$Destination\" + $_.BaseName

        # Create new folder under destination directory
        New-Item dirName -ItemType directory

        # Inform user of destination folder name
        Write-Host $dirName
    }
}
Sean Rhone
  • 250
  • 2
  • 7