Batch - Findstr - Escape chars

Viewed 46

I have a textfile/logfile called textexample.txt with a lot of lines which was generated from a 3rd Party Software and therefore does contain a lot of special characters.

Now im searching for some special keywords in this logfile and therefore i use findstr.

The problem is that i want to store every character which has to be escaped in a variable called charsToEscape, but this is not working proper. Is there any 'simple' solution for my example?

Here is the textfile "textexample.txt" im searching in:

17:09:47|          |                                    |OS ver      : 1.2 AB:0.0  CD:1234 KType:-1 
17:09:48|          |InitInstance()                      |Start copy files from Section=Files 
17:09:49|          |                                    |   ******************************  
17:09:50|          |StartExe                            |Here is the Value (1234) i am searching" 
17:09:50|          |StartExe                            |your path="C:\Some\Specific\Path" 
17:09:50|          |someRandomWords                     |WaitLevel="1" (default value) 

Here is my Batchfile:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

:MAIN
setlocal
::REM List with the expected Values im searching for..
set "expected_value_pool=9876 turn 1234 failure adios"
call :SearchValue "%expected_value_pool%" "readback_value"
if "!errorlevel!" equ "0" (
    echo readback=!readback_value!
)
pause
exit /b 0

REM Function, which checks if the give return value is in a specific textfile (line for line check)
:SearchValue
setlocal
set "_expected_value_pool=%~1"
set "_readback_value=%~2"
set "file=textexample.txt"
set "charsToEscape=/ \ > < ? | " - $ ( ) [ ] { } : = ."
set "escapeChar=^"
for /f "tokens=*" %%a in (%file%) do (
    set "act_line=%%a"
    for %%c in (%charsToEscape%) do (
        set "act_line=!act_line:%%c=%escapeChar%%%c!"
    )
    for %%i in (%_expected_value_pool%) do (
        echo !act_line!|findstr /r "^.*%%i.*$" >NUL
        if "!errorlevel!" equ "0" (
            (endlocal
                if "%_readback_value%" neq "" (set "%_readback_value%=%%i")
            )
        exit /b 0
        )
    )
)
exit /b 1

UPDATE:

i get the error ">" cant be processed syntactically at this point from the line set "charsToEscape=/ \ > < ? | " - $ ( ) [ ] { } : = ." due to the special characters which cannot be proper processed

0 Answers
Related