gpt4 book ai didi

file - Powershell 替换存储在多个文件夹中的多个文件中的文本

转载 作者:行者123 更新时间:2023-12-04 04:57:07 26 4
gpt4 key购买 nike

我想替换多个文件和文件夹中的文本。文件夹名称更改,但文件名始终为 config.xml。

$fileName = Get-ChildItem "C:\config\app*\config.xml" -Recurse
(Get-Content $fileName) -replace 'this', 'that' | Set-Content $fileName

当我运行上面的脚本时,它可以工作,但它将整个文本写入 config.xml 大约 20 次。怎么了?

最佳答案

$filename 是 System.IO.FileInfo objects 的集合.
您必须循环获取每个文件的内容:
这应该做你想做的:

$filename | %{
(gc $_) -replace "THIS","THAT" |Set-Content $_.fullname
}

关于file - Powershell 替换存储在多个文件夹中的多个文件中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21558726/

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