[Powershell]:第二次执行复制项将创建子文件夹(5.1.17763.1007)

[Powershell]:第二次执行复制项将创建子文件夹(5.1.17763.1007),powershell,copy-item,Powershell,Copy Item,在搜索wile后,我需要在此处发布我的问题: 我想做一个简单的任务: copy-item -path "C:\Folder Copied" -destination "C:\Folder Copied_New" -recurse 假设目录“Folder Copied\u New”不存在于“C:”,PS将创建一个文件夹,并将文件夹(及其内容)“C:\Folder Copied”复制到“Folder Copied\u New” 但是,如果再次执行该命令,则会

在搜索wile后,我需要在此处发布我的问题: 我想做一个简单的任务:

copy-item -path "C:\Folder Copied" -destination "C:\Folder Copied_New" -recurse
假设目录“Folder Copied\u New”不存在于“C:”,PS将创建一个文件夹,并将文件夹(及其内容)“C:\Folder Copied”复制到“Folder Copied\u New”

但是,如果再次执行该命令,则会发生以下情况:

Powershell已创建:“C:\Folder Copied\u New\Folder Copied”(内容“Test.txt”也已复制到此新创建的文件夹

第三次执行命令时,它会说文件夹已经存在

因此,我的问题是: 在我第二次运行该命令后,PS应该抛出一个错误,即“Folder Copied_New”。我该怎么做

我尝试复制和重命名新文件夹,在路径中使用或不使用“”,但没有任何效果。我确实想到使用-Testpath,但我想我向社区寻求一种更简单(最佳实践)的方法

提前感谢您的阅读和建议


不是答案,而是想写评论。 您能运行下面的脚本并向我们显示输出吗

$root = "$($env:TEMP)\test"
$source = "$($root)\Logfiles"
$destination = "$($root)\Drawings\Logs"

# Verify folders don't exist yet
if (Test-Path $source) {Throw}
if (Test-Path $destination) {Throw}

# Set up
New-Item $source, $destination -ItemType Directory -Force | Out-Null # Create folders
New-Item "$($source)\testfile" -ItemType File -Force | Out-Null # Create a testfile

# Show files/folders before copy
Write-Output "Before copy"
Get-ChildItem $root -Recurse | select fullname

# Copy first time
Copy-Item -path $source -destination $destination -Recurse -ErrorAction Continue
Write-Output "After first time copy"
Get-ChildItem $root -Recurse | select fullname

# Copy second time
Copy-Item -path $source -destination $destination -Recurse -ErrorAction Continue
Write-Output "After second time copy"
Get-ChildItem $root -Recurse | select fullname

# Copy third time
Copy-Item -path $source -destination $destination -Recurse -ErrorAction Continue
Write-Output "After third time copy"
Get-ChildItem $root -Recurse | select fullname

# Clean up
Remove-Item $source, $destination -Force -Recurse | Out-Null
在我的计算机上,我没有观察到任何异常行为。第一次运行后,源文件夹get被复制到目标。第二次运行不会改变任何内容,第三次运行也不会


该行为是默认行为:

假设您要复制并重命名一个项目,则执行以下操作:

Copy-Item -path source.txt -Destination destination.txt
您需要一个名为
destination.txt
的文件,其内容为
source.txt

如果要复制文件但不重命名,请执行以下操作:

Copy-Item -path source.txt -Destination C:\test
您希望文件
source.txt
C:\test
中显示。如果
C:\test
不存在,将创建该文件

现在,让我们尝试在目标不存在时复制文件夹

Copy-Item -path C:\test -Destination D:\toast 
目标不存在,因此您通过将源文件夹复制到目标来创建它。目标的内容将与源相同。文件夹
D:\toast
C:\test
相同,但在此过程中被重命名。
但是,如果提供源对象(文件夹)将位于的目标路径,则它将位于其中

 Copy-Item -path C:\test -Destination D:\toast 

D:\toast
确实存在于我们之前的操作中,因此将在其中创建一个
C:\test
副本:
D:\toast\test

对我来说,它显示了您的预期行为。您有什么Powershell版本?刚刚测试过,我没有观察到这种行为。您的问题中还缺少一些其他内容。您能告诉我们最小值吗在您的设备上复制问题的所有实际代码?@T-Me 5.1.17763。100@LievenKeersmaekers编辑问题,这样您就可以通过创建文件夹“C:\folder Copied”来测试它,我只是在文件夹中有一个Test.txt用于测试。@LievenKeersmaekers抱歉,我对powerscript和generel编程非常陌生。首先,感谢testscript。我刚刚学到了一些新东西。它按预期工作。但是,区别在于,您已经创建了目标,而在我的代码中,目标文件夹是作为PS创建的ing执行副本。请尝试您的测试脚本,我只修改了第10行:
New Item$source-ItemType Directory-Force | Out Null#Create folders
非常感谢您的解释。对我来说,这种行为似乎很好:我的想法是:如果此脚本的执行者运行它,然后再次运行它会怎么样他忘了已经运行了?第二次执行将触发复制到不需要的子文件夹中。由于我是一般编程的初学者,我想你可能知道一种我不知道的简单方法。cmdlet
copy Item
没有任何参数可以改变这一点。唯一的办法是首先创建父文件夹我希望有一个始终如一的行为。。。