Reputation: 319
I'm doing a project to help myself learn batch a little better. I have code which is meant to be a "UI" of sorts.
The snippet I have an issue with is this:
:install_check
if exist UI (
echo UI is already installed; reinstall?
CHOICE
if Errorlevel 1 goto :del
if Errorlevel 2 goto :return
cls && goto :install_check
)
goto :install_yes
I know the naming conventions and some of the coding stuff isn't perfect, but I really see no reason why every time it runs it goes to :del
no matter what I type in for choice
.
I've changed the choice
switches and moved it around to be all in one line but it still doesn't want to work. The if
that it is nested in is meant to check if a file is present on the computer, so it is a requirement but also may be the issue?
I tried looking around on this site but nothing will help my issue, any help is gladly accepted!!
Upvotes: 0
Views: 496
Reputation: 21
ERRORLEVEL doesn't update inside control blocks like IF statements unless you use !ERRORLEVEL! instead of %ERRORLEVEL% and use this command at the start of your code: setlocal ENABLEDELAYEDEXPANSION
see http://batcheero.blogspot.ca/2007/06/how-to-enabledelayedexpansion.html
Upvotes: 2
Reputation: 38623
As a solution has already been determined, the following example, (using your existing label names), restructures your code a little:
:install_check
ClS
If Not Exist "UI" GoTo install_yes
Choice /M "UI is already installed; reinstall"
If Errorlevel 2 GoTo return
GoTo del
If the next line/label in your code is :del
, you may also remove line six.
If the name UI
belonged to a directory instead of a file then you'd change the third line to If Not Exist "UI\" GoTo install_yes
.
Upvotes: 0
Reputation: 80033
Check the errorlevel
s in reverse-order.
if errorlevel n
means if errorlevel
is n or greater
hence, errorlevel
2 will be interpreted as true
for if errorlevel 1
Upvotes: 3