gpt4 book ai didi

PowerShell - 抑制 Copy-Item 'Folder already exists' 错误

转载 作者:行者123 更新时间:2023-12-02 22:34:44 27 4
gpt4 key购买 nike

当我运行递归时 Copy-Item从具有子文件夹的文件夹到包含与原始子文件夹相同的子文件夹的新文件夹,当子文件夹已存在时,它会引发错误。

我该如何抑制这种情况,因为它是误报,并且会使真正的失败更难看到?

示例:

Copy-Item "C:\realFolder\*" "C:\realFolder_new" -recurse

Copy-Item : Item with specified name C:\realFolder_new\subFolder already exists.

最佳答案

您可以 try catch 发生的任何错误,然后决定是否关心它:

Copy-Item "C:\realFolder\*" "C:\realFolder_new" -recurse -ErrorVariable capturedErrors -ErrorAction SilentlyContinue
$capturedErrors | foreach-object { if ($_ -notmatch "already exists") { write-error $_ } }

关于PowerShell - 抑制 Copy-Item 'Folder already exists' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9434875/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com