1

I want to check if count exists in mycountry or not, and then do some operations according.

My code snippet :

rem @ECHO OFF
cls
SET FILE="mycountry"

If true I want to run 3 statements and if false I want to run 3 other statements.

I have tried this combination:

Echo.%FILE% | findstr /C:"count">nul && (Echo.TRUE) || (Echo.FALSE)

But how to write multiple statements if the condition gets true? I don't wanna use any flag variable.

Below snippet is not working.

Echo.%FILE% | findstr /C:"count">nul && 
(
Echo.TRUE
echo "ran correct."
) 
|| (Echo.FALSE)
4
  • Do you want to check if a substring exists in a variable or in a textfile? Commented Dec 26, 2018 at 14:29
  • The snippet fails to work because the opening parenthesis of the code block has to be on the same line as the conditional execution && (... and also ) || (... BTW here it doesn't matter, but don't inject a space in front of the pipe symbol Echo.%FILE%| findstr Commented Dec 26, 2018 at 17:05
  • Correct @LotPings, however the OP also states that they've 'tried this combination', Echo.%FILE% | findstr /C:"count">nul && (Echo.TRUE) || (Echo.FALSE), which has parentheses on the correct line, and therefore does not exhibit the issue! My suspicion is that they've not provided accurate information in their question body. Commented Dec 26, 2018 at 17:40
  • I interpret I have tried this combination versus Below snippet is not working that the 1st did work, the latter not. Commented Dec 26, 2018 at 17:45

2 Answers 2

4

You can use the %errorlevel% value combined with an if/else.
See the example below:

REM @echo off
cls
SET FILE="mycountry"
SET STR="TEST"

findstr %STR% %FILE% >nul
if %errorlevel% equ 1 (
    goto searchError
) else (
    goto searchSucces
)

:searchSucces
echo String %STR% found in file %FILE%
pause
exit

:searchError
echo String %STR% not found in file %FILE%
pause
exit
Sign up to request clarification or add additional context in comments.

Comments

3

Your code, (integrated into a batch file), appears to work as expected:

@Echo Off

Set "FILE="

Set /P "FILE=Enter String: "

If Not Defined FILE Exit /B

Echo.%FILE% | findstr /C:"count">nul && (Echo.TRUE) || (Echo.FALSE)

Pause

In addition, the following two methods both appear to work as expected:

Using Echo and FindStr (as in your code):

@Echo Off

Set "FILE="

Set /P "FILE=Enter String: "

Echo=%FILE%|FindStr /IC:"count">Nul 2>&1&&(Echo TRUE
    Echo Ran correct.
    Timeout 3 /NoBreak>Nul
    Echo Still running!)||Echo FALSE
Pause

Using variable substitution:

@Echo Off

Set "FILE="

Set /P "FILE=Enter String: "

If /I "%FILE:count=%"=="%FILE%" (Echo FALSE) Else (Echo TRUE
    Echo Ran correct.
    Timeout 3 /NoBreak>Nul
    Echo Still running!)
Pause

If the examples above do not work for you, you should edit your question to include the actual code and strings you're using in your real world scenario. We cannot fix something we cannot see, especially if you don't fully explain the issue, (snippet is not working is a statement only, not an explanation).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.