Samselvaprabu
Samselvaprabu

Reputation: 18147

How to create folder structure skeleton using Powershell?

we are having folder/sub folder structure for our application.

Whenever we are adding new modules, we have to copy the folder structures exactly without copying files.

How to copy the folders and subfolders in the hierarchy but leaving the files alone?

Upvotes: 4

Views: 4216

Answers (2)

David Brabant
David Brabant

Reputation: 43459

$source = "<yoursourcepath>"
$destination = "<yourdestinationpath>"

Get-ChildItem -Path $source -Recurse -Force |
    Where-Object { $_.psIsContainer } |
    ForEach-Object { $_.FullName -replace [regex]::Escape($source), $destination } |
    ForEach-Object { $null = New-Item -ItemType Container -Path $_ }

Upvotes: 3

nimizen
nimizen

Reputation: 3419

This is a bit 'hacky' as Powershell doesn't handle this very well... received wisdom says to use xCopy or Robocopy but if you really need to use Powershell, this seems to work OK:

$src = 'c:\temp\test\'
$dest = 'c:\temp\test2\'

$dirs = Get-ChildItem $src -recurse | where {$_.PSIsContainer}

foreach ($dir in $dirs)
{
    $target = ($dir.Fullname -replace [regex]::Escape($src), $dest)

    if (!(test-path $target))
    {
         New-Item -itemtype "Directory" $target -force
    }
}

Upvotes: 5

Related Questions