1.

Solve : if exists a string in a file do this...?

Answer»

Hello,


Can someone help me with the syntax in a .BAT file to do the following?
If exists the string "hello" in a file ECHO hello exists
If not exists the string "hello" in a file echo hello does not exists
use findstr and errorlevel

findstr "hello" textfile
if %errorlevel% EQU 1 echo Not found

This is a similar version of what Ghostdog did, but I included the output for hello existing, and not existing (and I prefer to use if errorlevel instead of comparing the errorlevel environment variable). This ASSUMES you are running WINDOWS 2000 or NEWER and you want to find any case of "hello":

Code: [Select]@echo off
find /i "hello" file.txt >NUL
if errorlevel 1 (echo hello does not exist) else echo hello exists



Discussion

No Comment Found