Windows批处理文件 - 在变量上查找,查找包含百分比字符和单引号的字符串

时间:2015-11-06 11:23:14

标签: windows batch-file findstr

在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'"import bge import GameLogic import os os.system("cls") scene = GameLogic.getCurrentScene() objects = scene.objects objectCube = objects["Cube"] visible = objectCube.visible if visible == True: objectCube.setVisible(False, True) else: objectCube.setVisible(True, True) ...它有效

最佳

1 个答案:

答案 0 :(得分:1)

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

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

...到...

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

......它有效。

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