【问题标题】:How to create folder structure skeleton using Powershell?如何使用 Powershell 创建文件夹结构骨架?
【发布时间】:2013-09-09 15:17:26
【问题描述】:

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

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

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

【问题讨论】:

    标签: powershell powershell-2.0


    【解决方案1】:

    这有点“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
        }
    }
    

    【讨论】:

      【解决方案2】:
      $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 $_ }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-10
        • 2021-05-16
        • 2017-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-07
        相关资源
        最近更新 更多