programing

Powershell의 Copy-Item의 -container 인수의 의미는 무엇입니까?

mailnote 2023. 10. 13. 22:24
반응형

Powershell의 Copy-Item의 -container 인수의 의미는 무엇입니까?

저는 MS PowerShell의 스크립트를 작성하고 있습니다.이 스크립트는 다음을 사용합니다.Copy-Item지휘.이 명령의 선택적 인수 중 하나는 "입니다.-container". 인수에 대한 설명서에는 이 인수를 지정하는 "복사 작업 중에 컨테이너 개체를 보존합니다."라고 나와 있습니다.

복사 작업 중에 보존되지 않은 컨테이너 개체를 원하는 사람은 제가 마지막이기 때문에 이 모든 것이 좋습니다.하지만 진지하게 이 논쟁은 무엇을 하는 것일까요?특히 디스크 디렉토리 트리를 한 곳에서 다른 곳으로 복사하는 경우, 이것이 동작에 어떤 차이가 있습니까?Copy-Item지휘?

저도 그 문서가 도움이 되지 않는다고 생각했습니다.그 방법을 알아보기 위해 몇 가지 테스트를 해봤습니다.-Container매개 변수는 다음과 함께 작동합니다.-Recurse파일과 폴더를 복사할 때.

참고:-Container수단-Container: $true.

예제에 사용한 파일 구조는 다음과 같습니다.

#    X:.
#    ├───destination
#    └───source
#        │   source.1.txt
#        │   source.2.txt
#        │
#        └───source.1
#                source.1.1.txt
  • 모든 예에서 현재 위치(pwd)는X:\.
  • 파워쉘 4.0을 사용했습니다.

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.     

2) 전체 폴더 구조를 파일로 복사하려면:

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 (...)

문서에서 말하는 컨테이너는 폴더 구조입니다.재귀적 복사를 수행하고 폴더 구조를 유지하려면 -container 스위치를 사용합니다. (참고: 기본적으로 -container 스위치는 true로 설정되어 있으므로 지정할 필요가 없습니다.끄고 싶으시면 사용하실 수 있습니다.-container: $false.)

여기에는 뭔가 함정이 있습니다...디렉토리 목록을 작성하고 Copy-Item에 파이프링하면 폴더 구조가 보존되지 않습니다.폴더 구조를 유지하려면 -path 속성과 -recurse 스위치를 지정해야 합니다.

언급URL : https://stackoverflow.com/questions/129088/what-is-the-meaning-of-powershells-copy-items-container-argument

반응형