gpt4 book ai didi

Powershell Copy-Item - 仅当文件存在于目标中时才排除

转载 作者:行者123 更新时间:2023-12-03 22:18:46 25 4
gpt4 key购买 nike

以下是我的 powershell 脚本中的确切场景。

$Source = "C:\MyTestWebsite\"
$Destination = "C:\inetpub\wwwroot\DemoSite"
$ExcludeItems = @(".config", ".csproj")

Copy-Item "$Source\*" -Destination "$Destination" -Exclude $ExcludeItems -Recurse -Force

如果目标文件夹中不存在 .config 和 .csproj 文件,我希望此代码复制它们。当前脚本只是将它们排除在外,无论它们是否存在。目标是,我不希望脚本覆盖 .config 和 .csproj 文件,但如果目的地不存在它们,它应该复制它们。

知道脚本中需要进行哪些更正吗?

对此的任何帮助将不胜感激。

谢谢

最佳答案

这应该非常接近你想做的事情

$Source = "C:\MyTestWebsite\"
$Destination = "C:\inetpub\wwwroot\DemoSite"

$ExcludeItems = @()
if (Test-Path "$Destination\*.config")
{
$ExcludeItems += "*.config"
}
if (Test-Path "$Destination\*.csproj")
{
$ExcludeItems += "*.csproj"
}

Copy-Item "$Source\*" -Destination "$Destination" -Exclude $ExcludeItems -Recurse -Force

关于Powershell Copy-Item - 仅当文件存在于目标中时才排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26988716/

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