For searches in Windows Registry using batch scripting, I have to loop through a few keys, make a comparison to determine which is the right one, and then update the key.
Iteration in a for loop seems impossible to break out of. I have seen that others are facing similar issues but there does not seem to be a simple solution. Here is a snippet that demonstrates the issue.
@echo off
echo.
echo Diet Favorites
set favorite="bananas"
for %%a in (apples, bananas, chocolates) do call :reviewList %%a
echo.
echo Processing completed.
goto end
:reviewList item
set foundFavorite="false"
call :chooseFavorite "%~1"
if /I "%foundFavorite%"=="true" (
echo found the favorite - %~1
exit /b 0
) else (
echo skip %~1
)
endlocal & goto :eof
:chooseFavorite item
if "%~1"==%favorite% set "foundFavorite=true"
endlocal & goto :eof
:end
chooseFavorite returns the favorite in an environment variable. The output shows that reviewList continues looping after the favorite is identified.
Diet Favorites
skip apples
found the favorite - bananas
skip chocolates
Processing completed.
The comparison is working, but if the exit worked as expected, chocolates diet option should not be listed at all. How do I neatly break out of the loop iteration?