if a string exists in a txt file,return true

February 23, 2011 at 09:05:17
Specs: Windows XP
I want to check if a string exists in a text file using DOS/BATCH FILE

pseudo algorithm will be like this:

String searchString<--- "abc"

if(searchString exists in test.txt)
do this
else
do that


See More: if a string exists in a txt file,return true

Report •

#1
February 23, 2011 at 09:20:07
@echo off
set string=abc
type "test.txt" | find "%string%" > nul
if ErrorLevel 1 (
  echo.  FALSE
) else (
  echo.  TRUE
)

By the way this is NT batch scripting since there is no DOS in Windows XP rhat is NT kernel based.

Report •

#2
February 23, 2011 at 09:28:28
First of all,thanks a lot for your quick response.Need some more help on this.

1.I want to use this as a part of function which will return true/false
2.I will be using this as a part of batch file(.bat) which will be running in windows xp

Truly speaking,I have no idea of batch programming,a java programmer.SO if I am asking something silly please forgive me.


Report •

#3
February 23, 2011 at 09:40:41
In Batch scripts there is no "function" you can just call another .bat via a CALL statement i.e.

call MyRoutine Param1 Param2...

where MyRoutine is the batch MyRoutine.bat.

The problem is a batch can just set environment variables not return values in other ways. What you want to achieve is possible but there is no consolidated method to implement that, you need to setup the script according to your specific purposes.

The use of subroutines in batch is not an usual practice.


Report •
Related Solutions


Ask Question