Yohann Lopez
Yohann Lopez

Reputation: 21

Find a string included carriage return in a file - Windows batch

I'm trying to detect a string with 2 carriage return in a file using windows batch.

I know the file, and I need to know if the string I search is in it or not.

Here is the string :

[Terminal]
Fenetre=O
Debug=O

And here is the code I tried to use, but it doesn't work...

@echo off
findstr /M "[Terminal]\\r\\nFenetre=O\\r\\nDebug=O" C:\ETMI\ECSPyx\pyxvital\Pyxvital.ini
if %errorlevel%==0 (
    echo Found! logged files into results.txt
) else (
    echo No matches found 
)

The final result I need, is just to know if the file contains or not this "string", if not, I will write it on the file. Else, I will put a semicolon in front of 'Fenetre'

But first, how do I find this ?

Thank you !!

Upvotes: 0

Views: 1326

Answers (1)

user7818749
user7818749

Reputation:

I think you might find this post by dbenham useful. It might get you running something similar to this:

@echo off
setlocal
::Define LF variable containing a linefeed (0x0A)
set LF=^


::NOTE! the above 2 blank lines are critical - do not remove

::Define CR variable containing a carriage return (0x0D)
for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"

setlocal enableDelayedExpansion
::regex "!CR!*!LF!" will match both Unix and Windows style End-Of-Line
findstr /n /r /c:"\[Terminal\]!CR!*!LF!Fenetre=O!CR!*!LF!Debug=O" C:\ETMI\ECSPyx\pyxvital\Pyxvital.ini>nul && echo Found || echo Not found

Upvotes: 1

Related Questions