Windows 批处理文件 - findstr 一个变量,寻找包含百分比字符和单引号的字符串

Windows batch file - findstr over a variable, looking for string containing percent character and single quote

在 windows 批处理文件中,我正在检查作为输入给出的文本文件的内容 - 假设包含以下文本的单行文本文件 (test.txt):

"bla //ID_SCEN='%3' blabla"

其中一项检查包括使用 findstr 在行中查找特定字符串 (//ID_SCEN='%3')。测试代码如下(test.bat).

@echo off
set myLine=
(    
set /p myLine=
)<%1
echo Just found the following line in the input file:
echo %myLine%
echo %myLine% | findstr /C:"//ID_SCEN=^'%%3^'" 1>nul
if errorlevel 1 (
        echo The line %myLine% does not contain //ID_SCEN='%%3'
        echo Too bad, it is compulsory ... I quit
        GOTO:EOF
) else (
    echo Found expected stuff
)

现在 test.bat test.txt 的输出是:

Just found the following line in the input file:
"bla //ID_SCEN='%3' blabla"
The line "bla //ID_SCEN='%3' blabla" does not contain //ID_SCEN='%3'
Too bad, it is compulsory ... I quit

有人可以帮忙吗?它可能与转义字符有关 - 根据论坛帖子尝试了各种方法但尚未成功...

已接受的答案:

替换搜索字符串中单引号的 ^ 转义:"//ID_SCEN=^'%%3^'""//ID_SCEN='%%3'" ...有效

最佳

单引号不需要在 FINDSTR 中转义。改变...

echo %myLine% | findstr /C:"//ID_SCEN=^'%%3^'" 1>nul

...到...

echo %myLine% | findstr /C:"//ID_SCEN='%%3'" 1>nul

...它有效。

但是,请注意 FINDSTR 本身就是一团糟。它包含许多奇怪的边缘案例和未记录的行为。看看 dbenham's exhaustive investigation on the matter :)