Powershell,使用 contains 检查文件是否包含某个单词 [英] Powershell, using contains to check if files contain a certain word
问题描述
我正在尝试创建一个 powershell 脚本,它查看给定目录下的所有文件和文件夹,然后将 .properties 文件中给定单词的所有实例更改为另一个给定单词的实例.
我在下面写的内容是这样做的,但是我的版本控制会注意到每个文件中的更改,无论它是否包含要更改的单词的实例.为了解决这个问题,我尝试在获取/设置内容之前检查文件中是否存在该单词(如下所示),但是它告诉我 [System.Object[]] 不包含名为包含"的方法.我认为这意味着 $_ 是一个数组,所以我尝试创建一个循环来遍历它并一次检查每个文件,但是它告诉我它无法索引到 System.IO.FileInfo 类型的对象.>
谁能告诉我如何更改下面的代码以检查文件是否包含 wordToChange,然后应用它所做的更改.
$directoryToTarget=$args[0]$wordToFind=$args[1]$wordToReplace=$args[2]清除内容日志.txtGet-ChildItem -Path $directoryToTarget -Filter *.properties -Recurse |{ !$_.PSIsContainer } |%{If((Get-Content $_.FullName).Contains($wordToFind)){添加内容 log.txt $_.FullName(获取内容 $_.FullName) |ForEach-Object { $_ -replace $wordToFind , $wordToReplace } |设置内容 $_.FullName}}
谢谢!
试试这个:
$directoryToTarget=$args[0]$wordToFind=$args[1]$wordToReplace=$args[2]清除内容日志.txtGet-ChildItem -Path $directoryToTarget -Filter *.properties -Recurse |{ !$_.PSIsContainer } |%{$file = Get-Content $_.FullName$containsWord = $file |%{$_ -匹配 $wordToFind}如果($containsWord -包含 $true){添加内容 log.txt $_.FullName($文件) |ForEach-Object { $_ -replace $wordToFind , $wordToReplace } |设置内容 $_.FullName}}
这会将文件的内容放入一个数组 $file
中,然后检查每一行是否有单词.每行结果 ($true
/$false
) 被放入一个数组 $containsWord
.然后检查该数组是否找到该单词($True
变量存在);如果是,则运行 if 循环
.
I am trying to create a powershell script which looks through all files and folders under a given directory it then changes all instances of a given word in .properties files to an instance of another given word.
What I have written below does this however my version control notices a change in every single file regardless of whether it contains an instance of the word to change. To counter this I tried to put in a check to see whether the word is present or not in the file before I get/set the content (shown below) however it tells me that [System.Object[]] doesn't contain a method named 'Contains'. I assumed this meant that $_ was an array so I tried to create a loop to go through it and check each file at a time however it told me it was Unable to index into an object of type System.IO.FileInfo.
Could anyone please tell me how I could alter the code below to check if ever file contains the wordToChange and then apply the change it it does.
$directoryToTarget=$args[0]
$wordToFind=$args[1]
$wordToReplace=$args[2]
Clear-Content log.txt
Get-ChildItem -Path $directoryToTarget -Filter *.properties -Recurse | where { !$_.PSIsContainer } | % {
If((Get-Content $_.FullName).Contains($wordToFind))
{
Add-Content log.txt $_.FullName
(Get-Content $_.FullName) |
ForEach-Object { $_ -replace $wordToFind , $wordToReplace } |
Set-Content $_.FullName
}
}
Thank you!
Try this:
$directoryToTarget=$args[0]
$wordToFind=$args[1]
$wordToReplace=$args[2]
Clear-Content log.txt
Get-ChildItem -Path $directoryToTarget -Filter *.properties -Recurse | where { !$_.PSIsContainer } | % {
$file = Get-Content $_.FullName
$containsWord = $file | %{$_ -match $wordToFind}
If($containsWord -contains $true)
{
Add-Content log.txt $_.FullName
($file) | ForEach-Object { $_ -replace $wordToFind , $wordToReplace } |
Set-Content $_.FullName
}
}
This will put the content of the file into an array $file
then check each line for the word. Each lines result ($true
/$false
) is put into an array $containsWord
. The array is then check to see if the word was found ($True
variable present); if so then the if loop
is run.
这篇关于Powershell,使用 contains 检查文件是否包含某个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!