Copy-Item 在 运行 第二次时复制目标内的文件夹
Copy-Item copies folder inside the target when running second time
在我的内部系统上重复收集日志期间,发现递归 Copy-Item 调用的奇怪行为
比如说,我在 C:\Source 中有一些文件和子文件夹。我想将其递归复制到 C:\Target。第一次将所有源代码递归复制到 C:\Target
当我第二次尝试将 "C:\Source" 复制到 "C:\Target" 时出现问题。
在这种情况下,将 "C:\Source" 复制到 "C:\Target\Source"
例如:
cls
if(Test-Path "C:\Target")
{
Remove-Item "C:\Target"
}
Copy-Item "C:\Source" "C:\Target" -Force -Recurse | Out-Null
DIR "C:\Target"
Write-Host "OK"
Write-Host " "
Write-Host " "
Copy-Item "C:\Source" "C:\Target" -Force -Recurse | Out-Null
DIR "C:\Target"
Write-Host "Not OK"
脚本输出:
Directory: C:\Target
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 4/11/2016 3:45 PM SampleSourceFolderLevel1
-a--- 4/11/2016 3:35 PM 0 SampleFileLevel0.txt
OK
d---- 4/11/2016 3:45 PM SampleSourceFolderLevel1
d---- 4/11/2016 3:45 PM Source
-a--- 4/11/2016 3:35 PM 0 SampleFileLevel0.txt
Not OK
知道如何让 Copy-Item 像
一样工作吗
ROBOCOPY $sourceLog $targetLog /E | Out-Null
??
提前致谢
您似乎想复制 c:\Source
的内容。您只需要添加 \*
:
Copy-Item -Path "C:\Source\*" -Destination "C:\Target" -Force -Recurse | Out-Null
它适用于第一个 运行,因为您删除了目标文件夹并且 cmdlet 现在将文件夹 C:\Source
复制到 C:\Target
。如果 C:\Target
存在,cmdlet 会将源 复制到 目标文件夹。
Copy-Item
根据目的地存在与否存在不一致的行为。
如果您想获得一致的复制体验,请使用此模式:
$copyFrom = 'C:\Foo\Bar\TargetDirectoryToCopy'
$destinationPath = 'D:\Bar\Foo\'
$newItemParams = @{
Path = $destinationPath
ItemType = 'Directory'
Force = $True
}
$copyParams = @{
Path = $copyFrom
Destination = $destinationPath
Recurse = $True
Confirm = $false
Force = $True
}
New-Item @newItemParams
Copy-Item @copyParams
结果
目录 D:\Bar\Foo\
将嵌套 TargetDirectoryToCopy
目录。
因此,如果存在文件 C:\Foo\Bar\TargetDirectoryToCopy\FooBar.txt
,其副本将与 C:\Foo\Bar\TargetDirectoryToCopy
中的任何其他文件一起创建为 D:\Bar\Foo\TargetDirectoryToCopy\FooBar.txt
在我的内部系统上重复收集日志期间,发现递归 Copy-Item 调用的奇怪行为
比如说,我在 C:\Source 中有一些文件和子文件夹。我想将其递归复制到 C:\Target。第一次将所有源代码递归复制到 C:\Target
当我第二次尝试将 "C:\Source" 复制到 "C:\Target" 时出现问题。 在这种情况下,将 "C:\Source" 复制到 "C:\Target\Source"
例如:
cls
if(Test-Path "C:\Target")
{
Remove-Item "C:\Target"
}
Copy-Item "C:\Source" "C:\Target" -Force -Recurse | Out-Null
DIR "C:\Target"
Write-Host "OK"
Write-Host " "
Write-Host " "
Copy-Item "C:\Source" "C:\Target" -Force -Recurse | Out-Null
DIR "C:\Target"
Write-Host "Not OK"
脚本输出:
Directory: C:\Target
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 4/11/2016 3:45 PM SampleSourceFolderLevel1
-a--- 4/11/2016 3:35 PM 0 SampleFileLevel0.txt
OK
d---- 4/11/2016 3:45 PM SampleSourceFolderLevel1
d---- 4/11/2016 3:45 PM Source
-a--- 4/11/2016 3:35 PM 0 SampleFileLevel0.txt
Not OK
知道如何让 Copy-Item 像
一样工作吗ROBOCOPY $sourceLog $targetLog /E | Out-Null
??
提前致谢
您似乎想复制 c:\Source
的内容。您只需要添加 \*
:
Copy-Item -Path "C:\Source\*" -Destination "C:\Target" -Force -Recurse | Out-Null
它适用于第一个 运行,因为您删除了目标文件夹并且 cmdlet 现在将文件夹 C:\Source
复制到 C:\Target
。如果 C:\Target
存在,cmdlet 会将源 复制到 目标文件夹。
Copy-Item
根据目的地存在与否存在不一致的行为。
如果您想获得一致的复制体验,请使用此模式:
$copyFrom = 'C:\Foo\Bar\TargetDirectoryToCopy'
$destinationPath = 'D:\Bar\Foo\'
$newItemParams = @{
Path = $destinationPath
ItemType = 'Directory'
Force = $True
}
$copyParams = @{
Path = $copyFrom
Destination = $destinationPath
Recurse = $True
Confirm = $false
Force = $True
}
New-Item @newItemParams
Copy-Item @copyParams
结果
目录 D:\Bar\Foo\
将嵌套 TargetDirectoryToCopy
目录。
因此,如果存在文件 C:\Foo\Bar\TargetDirectoryToCopy\FooBar.txt
,其副本将与 C:\Foo\Bar\TargetDirectoryToCopy
D:\Bar\Foo\TargetDirectoryToCopy\FooBar.txt