findstr multiple lines problem

March 18, 2012 at 03:24:01
Specs: Windows 7
I need to be able to search and determine whether the following text is already in the hosts file:
if it is, goto A if not add it.
How can this be done by using the findstr command? since the're multiple lines?

See More: findstr multiple lines problem

Report •

March 20, 2012 at 00:58:31
It is not clear what you are searching for.

I think the following would do a logical OR with all these strings. Sounds like you want a logical AND.

findstr /G:strings.txt testfile.txt > nul

The errorlevel will tell you if any of them were found. Do you want to add only the lines that are missing? If you mean you need to have all those lines in that order, then it is a quite different problem.

Report •

March 22, 2012 at 07:28:55
Put the entries you want to search for in file2.txt, and change file.txt to your hosts file.

@echo off & setlocal enabledelayedexpansion
for /f "tokens=*" %%i in ('type file2.txt') do (
    findstr /c:"%%i" file.txt> nul
    if !errorlevel! neq 0 type nul|echo %%i>> file.txt

Report •
Related Solutions

Ask Question