简体   繁体   中英

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?

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
    }
}
$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 $_ }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM