简体   繁体   English

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

[英]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: 这有点'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
    }
}
$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 $_ }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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