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
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
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)
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>...
-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...
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...
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
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
> >
Hi Al
Cool approach :-)
Jens