%errorlevel% goto not working

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
awd777
Posts: 3
Joined: 22 Mar 2014 05:16

%errorlevel% goto not working

#1 Post by awd777 » 22 Mar 2014 05:19

Hi all,
Not particularly advanced in batch file scripting but just trying my hand. Having a bit of an issue with the following script:

Echo on
findstr /i "test" "c:\test\Test.txt" || if %errorlevel% EQU 1 goto notfound
findstr /i "test" "c:\test\Test.txt" || if %errorlevel% EQU 0 goto found

:notfound
start X.exe
goto done

:found
start Y.exe
goto done

:done
exit

I'm probably doing something stupid, but the original "goto" seems not to work?
Basically, what I want to happen is if the file located at c:\test\test.txt contains the word "text" then Y.exe starts, and if it doesn't, then X.exe starts.
Any assistance would be greatly appreciated.
Thanks,

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: %errorlevel% goto not working

#2 Post by foxidrive » 22 Mar 2014 05:25

|| and && are conditional operators.


|| executes on errorlevel 1
&& executes on errorlevel 0


Edited as mentioned by aGerman in the posts below.

This should work:

Code: Select all

Echo on
findstr /i "test" "c:\test\Test.txt" && (goto notfound) || (goto found)

:notfound
start X.exe
goto done

:found
start Y.exe
goto done

:done
exit


Or just use this

Code: Select all

@echo off
findstr /i "test" "c:\test\Test.txt" && (start "" x.exe) || (start "" y.exe)

aGerman
Expert
Posts: 4744
Joined: 22 Jan 2010 18:01
Location: Germany

Re: %errorlevel% goto not working

#3 Post by aGerman » 22 Mar 2014 05:52

@foxidrive Always use && first.

If you apply both && and || be careful! You could run into problems if the command in the first branch fails.
E.g.

Code: Select all

echo X | find "X" && (
  echo X found | find "x" >nul
) || (
  echo X not found
)


Better use the old school if errorlevel 1 statement.

Code: Select all

findstr /i "test" "c:\test\Test.txt" 
if errorlevel 1 (start "" x.exe) else start "" y.exe

Regards
aGerman

awd777
Posts: 3
Joined: 22 Mar 2014 05:16

Re: %errorlevel% goto not working

#4 Post by awd777 » 22 Mar 2014 05:59

Hi all,

Thanks for the replies. Much appreciated.

findstr /i "test" "c:\test\Test.txt"
if errorlevel 1 (start "" x.exe) else start "" y.exe

- This batch file seems to get stuck on a loop of constantly running findstr /i "test" "c:\test\Test.txt"


@echo off
findstr /i "test" "c:\test\Test.txt" || (start "" x.exe) && (start "" y.exe)

- This batch file actually launches both applications rather than X OR Y.

Any ideas on how to resolve?

aGerman
Expert
Posts: 4744
Joined: 22 Jan 2010 18:01
Location: Germany

Re: %errorlevel% goto not working

#5 Post by aGerman » 22 Mar 2014 06:17

As already mentioned you have to write the && first.

Code: Select all

findstr /i "test" "c:\test\Test.txt" && (start "" y.exe) || (start "" x.exe)


findstr /i "test" "c:\test\Test.txt"
if errorlevel 1 (start "" x.exe) else start "" y.exe

- This batch file seems to get stuck on a loop of constantly running findstr /i "test" "c:\test\Test.txt"

I don't know why that should happen. There is no loop at all. It works for me.

Regards
aGerman

awd777
Posts: 3
Joined: 22 Mar 2014 05:16

Re: %errorlevel% goto not working

#6 Post by awd777 » 22 Mar 2014 06:23

Apologies,

My machine seemed to be having a "moment" - works fine :)

Thank you very much for your help.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: %errorlevel% goto not working

#7 Post by foxidrive » 22 Mar 2014 22:06

aGerman wrote:@foxidrive Always use && first.

If you apply both && and || be careful! You could run into problems if the command in the first branch fails.


Thanks aGerman. Point noted.

Post Reply