powershell - 即使没有匹配,也要匹配
问题描述
我正在尝试在 powershell 中使用 -match 查找字符串“ErrorCode”:1。我发现了许多带有 ErrorCode:1 的文件。问题是,当我打开一个由于匹配表达式而给出的文件进行检查时,我什至没有找到 ErrorCode 这个词,但文件名仍然作为输出提供给我
我试图检查文件,但许多文件都有我正在搜索的字符串,但很多文件没有。
$fileNames = Get-ChildItem -Path $scriptPath -Recurse -Include *.data
$FoldersToRename = @() #initialize as array
foreach ($file in $fileNames) {
If (Get-Content $file | %{$_ -match '"ErrorCode":1'})
# If (Get-Content $file | %{$_ -match '"ErrorCode": [1-9]\d*'})
{
$FoldersToRename += Split-Path $file
echo $file
}
}
这是我作为输出得到的文件内容之一
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Not Found</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Not Found</h2>
<hr><p>HTTP Error 404. The requested resource is not found.</p>
</BODY></HTML>
有人可以帮助解释这个文件是如何输出的吗?
解决方案
从我之前对您的问题的回答中,您已经有了一个可行的解决方案。这里的问题是Get-Content $file | %{$_ -match '"ErrorCode": [1-9]\d*'}
返回一个数组(true
或false
值),所以if
语句总是true
.
要么接受我之前的建议,要么接受Mike 的建议
推荐阅读
- python - 正则表达式以特定格式匹配当前日期
- python - 将图像从 BGR 更改为 RGB
- react-native - 在解析模块 `react-native/Libraries/NewAppScreen` 时,发现了 Haste 包 `react-native`
- javascript - 在另一个内部创建一个 web 组件并将其附加到另一个上面是不好的做法吗?
- angular - 设置材料表中数组属性中的值
- unit-testing - 有什么方法可以使用 Jest 和 React 测试具有大型数据集的 highcharts。
- mysql - SQL多查询
- data-structures - 每次活动调用的 Power BI 可视化
- reactjs - Visual Studio Code / React - 是否有可能在某处显示所有可能的道具?
- c++ - 如何从文件中检索链表(在 Turbo C++ 中)