PowerShell可以替换存储在多个文件夹中的多个文件中的文本 [英] Powershell to replace text in multiple files stored in many folders
本文介绍了PowerShell可以替换存储在多个文件夹中的多个文件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想替换多个文件和文件夹中的文本。文件夹名称会更改,但文件名总是config.xml。
I want to replace a text in multiple files and folders. The folder name changes, but the filename is always config.xml.
$fileName = Get-ChildItem "C:\config\app*\config.xml" -Recurse
(Get-Content $fileName) -replace 'this', 'that' | Set-Content $fileName
当我运行上面的脚本时,它可以工作, config.xml约20次。什么是错的?
When I run the above script it works, but it writes the whole text in config.xml about 20 times. What's wrong?
推荐答案
$ filename是 System.IO.FileInfo对象的集合
。
您必须循环才能获取每个文件的内容:
这应该做你想做的:
$filename is a collection of System.IO.FileInfo objects
.
You have to loop to get the content for each file :
this should do what you want :
$filename | %{
(gc $_) -replace "THIS","THAT" |Set-Content $_.fullname
}
这篇关于PowerShell可以替换存储在多个文件夹中的多个文件中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文