English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I want to write a script to read from a txt file whether the file contents my specified string or not then echo yes or no.

2007-11-27 21:27:12 · 2 answers · asked by reza 1 in Computers & Internet Programming & Design

2 answers

The "find" command can search a file or a group of files for a specified string. If we combine the output of the "find" command with either the "&&" or "||" functions, then you can get exactly what you need.

First part,

find /i "textstring" filename.txt

The "/i" switch is to ignore the case of the search string. I find this useful if I'm not sure exactly what the case might be in a given file.

Second part
&& - means "if the prior command succeeded, then do the next thing"

|| - (that's a 'double pipe' the thing above the \ ), means "if the prior command failed, then do the next thing"

Which you use is up to you.

Here's how a test batch would look. It's two simple lines:

=========
find /i "text" myfile.txt
&& (echo YES & goto :EOF)
echo NO
=========

... Yahoo will wrap the first line onto two lines ...
Line 1 begins with the 'find'
Line 2 is simply the "echo NO'
=========

It looks for the string "text" in the file "myfile.txt", if it finds the string, then it will echo "YES" to the screen and then goto EOF (end of file). If it doesn't find the string, then it will fall to the next line and echo NO to the screen.

2007-11-30 11:00:36 · answer #1 · answered by Kevin 7 · 4 0

for /f "tokens=1" %%i in (c:\example.txt) do set yesno=%%i
if %yesno%==yes goto yes
echo no
exit
:yes
echo yes

2007-11-28 05:33:10 · answer #2 · answered by Anonymous · 0 0

fedest.com, questions and answers