In a windows batch file, I am checking the content of a text file given as input - let say a one line text file (test.txt) containing this text:
"bla //ID_SCEN='%3' blabla"
One of these checks includes looking for a specific string (//ID_SCEN='%3') in a the line with findstr. A test code is the following (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
)
For now the output of test.bat test.txt is:
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
Can someone help ? It probably has to do with the escape characters - tried various things based on forum posts but did not succeed yet ...
Accepted answer:
Replace the ^
escape of single quotes in the search string: "//ID_SCEN=^'%%3^'"
to "//ID_SCEN='%%3'"
... it worked
Best
Single quotes don't need escaping in FINDSTR
. Change...
echo %myLine% | findstr /C:"//ID_SCEN=^'%%3^'" 1>nul
...to...
echo %myLine% | findstr /C:"//ID_SCEN='%%3'" 1>nul
...and it works.
However, be aware that FINDSTR
per se is a mess. It comprises a host of weird edge-cases and undocumented behavior. Have a look at dbenham's exhaustive investigation on the matter :)