gpt4 book ai didi

powershell - 通过Get-ChildItem仅忽略根目录中的文件

转载 作者:行者123 更新时间:2023-12-02 23:27:20 24 4
gpt4 key购买 nike

我有一个非常简单的ASP.NET MVC应用程序目录结构,示例如下:

root/
----- views/
---------- index.cshtml
---------- web.config
----- scripts/
---------- main.js
---------- plugin.js
----- web.config

给定这种目录结构,我有一个小的Powershell脚本,该脚本可复制 [sourceDir]中的所有内容,忽略一些文件,然后将其复制到 [targetDir]中。

我在使用 [sourceDir] cmdlet复制 Get-ChildItem中的所有内容的第一步时遇到了问题。这是我的示例脚本(为简洁起见进行了编辑):
Get-ChildItem [sourceDir] -Recurse -Exclude web.config | Copy-Item -Destination [targetDir]

问题在于 -Exclude参数排除了根目录中的web.config和 View 目录中的web.config。从技术上讲,它会忽略每个web.config。但是,我只想忽略根目录中的文件。

是否可以通过 Get-ChildItem仅忽略根目录中的web.config?如果没有,我应该使用哪个cmdlet?



如建议的那样,为Where Linq子句放弃-Exclude参数是正确的解决方案。实际上,我有一个要忽略的文件数组,所以我使用了 -NotIn运算符而不是 -NotMatch样本脚本:
Get-ChildItem [sourceDir] -Recurse | 
Where { $_.FullName -NotIn $_filesToIgnore } |
Copy-Item -Destination [targetDir]

最佳答案

Get-ChildItem上的-Exclude参数一直是臭名昭著的问题根源。尝试这种方式:

Get-ChildItem [sourceDir] | 
Where {$_.FullName -notmatch "$sourceDirVar\\web\.config"} |
Copy-Item -Destination [targetDir] -Recurse

关于powershell - 通过Get-ChildItem仅忽略根目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31331562/

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