Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 611
Samselvaprabu Avatar asked Feb 19 '23 23:02

Samselvaprabu


2 Answers

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
    }
}
like image 111
nimizen Avatar answered Feb 23 '23 05:02

nimizen


$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 $_ }
like image 27
David Brabant Avatar answered Feb 23 '23 04:02

David Brabant