代码之家  ›  专栏  ›  技术社区  ›  Mark Meuer

Powershell的复制项的-container参数的含义是什么?

  •  30
  • Mark Meuer  · 技术社区  · 18 年前

    Copy-Item 命令此命令的可选参数之一是“ -container 。参数的文档说明,指定此参数“在复制操作期间保留容器对象”

    这一切都很好,因为在复制操作期间,我将是最不希望使用未准备好的容器对象的人。但说真的,这个论点有什么用呢?特别是在将磁盘目录树从一个地方复制到另一个地方的情况下,这会对 复制项目 命令

    2 回复  |  直到 8 年前
        1
  •  76
  •   bouvierr Danilo J. Bonsignore    10 年前

    我也发现这些文档没有什么帮助。我做了一些测试来看看 -Container 参数与 -Recurse

    -容器 -Container: $true .

    #    X:.
    #    ├───destination
    #    └───source
    #        │   source.1.txt
    #        │   source.2.txt
    #        │
    #        └───source.1
    #                source.1.1.txt
    
    • 对于所有示例,当前位置(pwd)为 X:\ .

    1) 要仅复制源文件夹(空文件夹),请执行以下操作:

    Copy-Item -Path source -Destination .\destination
    Copy-Item -Path source -Destination .\destination -Container
    #    X:.
    #    ├───destination
    #    │   └───source
    #    └───source (...)
    

    下面给出了一个错误:

    Copy-Item -Path source -Destination .\destination -Container: $false
    # Exception: Container cannot be copied to another container. 
    #            The -Recurse or -Container parameter is not specified.     
    

    Copy-Item -Path source -Destination .\destination -Recurse
    Copy-Item -Path source -Destination .\destination -Recurse -Container
    #    X:.
    #    ├───destination
    #    │   └───source
    #    │       │   source.1.txt
    #    │       │   source.2.txt
    #    │       │
    #    │       └───source.1
    #    │               source.1.1.txt
    #    └───source (...)    
    

    3) 要将所有子体(文件和文件夹)复制到单个文件夹中,请执行以下操作:

    Copy-Item -Path source -Destination .\destination -Recurse -Container: $false
    #    X:.
    #    ├───destination
    #    │   │   source.1.1.txt
    #    │   │   source.1.txt
    #    │   │   source.2.txt
    #    │   │
    #    │   └───source.1
    #    └───source (...)
    
        2
  •  35
  •   Tohuw    13 年前

    文档讨论的容器是文件夹结构。如果您正在执行递归复制并希望保留文件夹结构,则可以使用-container开关。(注意:默认情况下,-container开关设置为true,因此确实不需要指定它。如果要关闭它,可以使用 -container: $false .)

    推荐文章