Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need help with FINDSTR nested in IF command

9,151 views
Skip to first unread message

Scott Jerome-Parks

unread,
Apr 29, 2003, 4:31:25 PM4/29/03
to
Hi,

I'm trying to use the FINDSTR command within an IF construct.
Basically my command looks like:

IF "FINDSTR "MYSTRING" "MYLOGFILE.TXT""
DOTHIS
ELSE
DOTHAT

Explanation: If findstr returns with a successfull status (i.e. it
found the string MYSTRING in the file MYLOGFILE.TXT), then I want it
to branch to and execute the DOTHIS section, otherwise, branch to and
execute the DOTHAT section. I keep running into syntax errors. I've
tried many permutations, including escaping the quotes with ^, but to
no avail. I'm not even sure if I can use the IF command in this
fashion. Any ideas?

Thanks,
Scott

William Allen

unread,
Apr 29, 2003, 5:21:33 PM4/29/03
to
"Scott Jerome-Parks" wrote in message

Why not test the ERRORLEVEL returned by FINDSTR? Use the
mnemonic: ERRORLEVEL 0 = f0und ERRORLEVEL 1 - m1ssing

====Begin cut-and-paste (omit this line)
@ECHO OFF
findstr "MyString" MYLOGFILE.TXT>NUL
IF ERRORLEVEL 1 (GOTO NOTFOUND) ELSE (GOTO FOUND)
GOTO :EOF

:NOTFOUND
ECHO/ Action for Not Found condition
GOTO :EOF

:FOUND
ECHO/ Action for Found condition

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

--
William and Linda Allen
Creative Technical Writing - Allen & Company: http://www.allenware.com/
Free MS-DOS Batch File Course http://www.allenware.com/find?BatchCourse


Ted Davis

unread,
Apr 29, 2003, 8:18:55 PM4/29/03
to
On 29 Apr 2003 13:31:25 -0700, scott.je...@us.cibc.com (Scott
Jerome-Parks) wrote:

You should consult the on-line help for the commands that give
trouble. If you had checked IF /?, you would have seen that there are
only three test scenarios:

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command

The first tests the value of the previous program's exit code (with a
rather strange equal-to-or-greater-than test); the second compares
strings; and the third looks for files - none of these test the exit
code of a program that hasn't already terminated.

Those multiple quotes would confuse any command parser.

You have to run the FINDSTR command, then check its exit code with IF
ERRORLEVEL, or in some situations

(FINDSTR "MYSTRING" "MYLOGFILE.TXT" > nul) && do something

might be usable - it executes the command following && if and only if
the first command returns an exit code of 0.


T.E.D. (tda...@gearbox.maem.umr.edu - e-mail must contain "T.E.D." or my .sig in the body)

Scott Jerome-Parks

unread,
Apr 30, 2003, 9:41:34 AM4/30/03
to
Thanks for your help guys. The ERRORLEVEL approach looks much easier.

RTFM eh? :) As it was, I was using an NT shell scripting book which
listed more options for the if command. I guess I should have used IF
/? just to make sure.


Ted Davis <tda...@gearbox.maem.umr.edu> wrote in message news:<iu4uav896pm3clegl...@4ax.com>...

guard

unread,
Apr 30, 2003, 12:51:21 PM4/30/03
to
See the "IF Page" at
http://TheSystemGuard.com/TheGuardBook/CCS-Int/IF.htm)
and the "FindStr Page" at
(http://TheSystemGuard.com/TheGuardBook/CCS-Ext/FindStr.htm)
for combined NT/2K/XP/K3 syntax listings.

-tsg
____________________________________________________________
TheSystemGuard.com | BoomingOrFuming.com | MountCommands.com
Free and "Almost Free" Knowledge for Windows System Admins!

"Scott Jerome-Parks" <scott.je...@us.cibc.com> wrote in message
news:bc8dc734.03042...@posting.google.com...

David Candy

unread,
Apr 30, 2003, 1:14:27 PM4/30/03
to
findstr "dogggg" .\*.*&&echo sucess

To branch if found (dual & seperate commands)
findstr "dogggg" .\*.*&&Goto alabel

To branch if not found (dual pipes)
findstr "dogggg" .\*.*||Goto alabel

.\*.* means all files in current folder

--
http://www.g2mil.com/Apr2003.htm
http://prorev.com/forbesrussia.htm
---------------------------------------------------------------
David Candy
http://www.mvps.org/serenitymacros
---------------------------------------------------------------
"guard" <^T^S^G^n^e^w^s^@TheSystemGuard.com> wrote in message news:b8ouuh$c7dh9$1...@ID-170595.news.dfncis.de...

Jens Hastrup

unread,
Apr 30, 2003, 3:03:58 PM4/30/03
to
"Scott Jerome-Parks" <scott.je...@us.cibc.com> skrev i en meddelelse
news:bc8dc734.03042...@posting.google.com...

Hi Scott

Another approach very similar to the ones already suggested.....Using labels
... works on W2K and XP

::start suggestion
@echo off
findstr /i /c:"mystring" mylogfile.txt >nul
call :%errorlevel%
::some other commands
goto :EOF

:0
echo Found
goto :EOF

:1
echo Not found
goto :EOF
::end suggestion


Jens


Al Dunbar

unread,
Apr 30, 2003, 5:13:04 PM4/30/03
to

"Jens Hastrup" <jens-h...@NOSPAMoncable.dk> wrote in message
news:3eb01e23$0$42551$edfa...@dread11.news.tele.dk...

In case you want to use the same trick at other parts of the code, you could
prefix the labels with something different, i.e. :check1_1, :check1_2,
:check2_1, and :check2_2. Or you could avoid calls altogether with:

if errorlevel 1 (
echo/not found
) else (
echo/found
)

/Al

Jens Hastrup

unread,
May 1, 2003, 7:06:51 AM5/1/03
to

"Al Dunbar" <Luigi...@hotmail.com> skrev i en meddelelse
news:A%Wra.236652$vs.20...@news3.calgary.shaw.ca...

>
> "Jens Hastrup" <jens-h...@NOSPAMoncable.dk> wrote in message
> news:3eb01e23$0$42551$edfa...@dread11.news.tele.dk...

> >

Hi Al

Cool approach :-)

Jens


0 new messages