【问题标题】:How can Powershell copy an entire folder structure but exclude one folder and its contentsPowershell如何复制整个文件夹结构但排除一个文件夹及其内容
【发布时间】:2015-02-13 11:01:07
【问题描述】:

这似乎是一个简单的操作,但我不知道如何让 Powershell 将整个文件夹结构从一个位置复制到另一个位置,但排除一个文件夹(称为“连接”)及其内容。

我尝试过像这样组合 Copy-Item 和 Get-ChildItem

cpi (gci folder1 -Exclude connections) folder2  -recurse

但似乎 -recurse 参数覆盖了 -exclude 参数,并且连接文件夹及其内容被复制。如果没有 -recurse,我想要复制的文件夹的内容将被忽略。

【问题讨论】:

    标签: powershell powershell-3.0


    【解决方案1】:

    我不确定为什么这不起作用,它似乎在我的机器上运行正常。

    你总是可以通过管道发送到Copy-Item:

    Get-ChildItem folder1 | where { !(($_ -is [System.IO.DirectoryInfo]) -and ($_.Name -eq "connections")) } | Copy-Item -Destination folder2 -Recurse
    

    这样做的好处是你可以让PowerShell在之后打印输出:

    Get-ChildItem folder1 | where { !(($_ -is [System.IO.DirectoryInfo]) -and ($_.Name -eq "connections")) }
    

    这样您就可以准确检查正在复制的内容(即“连接”文件夹是否丢失?)

    【讨论】:

    • 有一个警告,它可以省略一个名为“connections”的文件,尽管这不太可能。还要避免在答案中使用像 dir 这样的别名,因为有些用户可能不熟悉它们。
    猜你喜欢
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    相关资源
    最近更新 更多