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,
%errorlevel% goto not working
Moderator: DosItHelp
Re: %errorlevel% goto not working
|| and && are conditional operators.
|| executes on errorlevel 1
&& executes on errorlevel 0
Edited as mentioned by aGerman in the posts below.
This should work:
Or just use this
|| 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)
Re: %errorlevel% goto not working
@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.
Better use the old school if errorlevel 1 statement.
Regards
aGerman
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
Re: %errorlevel% goto not working
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?
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?
Re: %errorlevel% goto not working
As already mentioned you have to write the && first.
I don't know why that should happen. There is no loop at all. It works for me.
Regards
aGerman
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
Re: %errorlevel% goto not working
Apologies,
My machine seemed to be having a "moment" - works fine
Thank you very much for your help.
My machine seemed to be having a "moment" - works fine

Thank you very much for your help.
Re: %errorlevel% goto not working
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.