如何使用Powershell创建文件夹结构框架?

时间:2012-07-05 11:42:11

标签: powershell powershell-v2.0

我们的应用程序有文件夹/子文件夹结构。

每当我们添加新模块时,我们都必须完全复制文件夹结构而不复制文件。

如何复制层次结构中的文件夹和子文件夹,但不保留文件?

2 个答案:

答案 0 :(得分:5)

这有点'hacky',因为Powershell不能很好地处理这个问题......收到的智慧说使用xCopy或Robocopy但是如果你真的需要使用Powershell,这似乎工作正常:

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

答案 1 :(得分:3)

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