user2064000
user2064000

Reputation:

Strange behaviour of if

I'm possibly being dumb here, but I simply cannot figure the strange behaviour of this piece of batch code that I have.

So I have the following piece of code:

@echo off

set exitval=0

:selectdir
echo Type the name of the folder where the software is installed (or drag and
echo drop the folder here):

set /p sdir=
set sdir=%sdir:"=%

echo.
if not exist "%sdir%\Lib\Plugins" (
    echo The plugins directory does not exist! Try again ^(y/N^)^?
    set /p c=

    if "%c%" == "y" goto selectdir

    goto exitscript
)

:exitscript
pause
exit /b %exitval%

Which does not show consistent behaviour, to say the least:

D:\Development>test.cmd
Type the name of the folder where the software is installed (or drag and
drop the folder here):
C:\xyz

The plugins directory does not exist! Try again (y/N)?
y
Press any key to continue . . .

D:\Development>test.cmd
Type the name of the folder where the software is installed (or drag and
drop the folder here):
C:\xyz

The plugins directory does not exist! Try again (y/N)?
n
Type the name of the folder where the software is installed (or drag and
drop the folder here):
C:\xyz

The plugins directory does not exist! Try again (y/N)?
n
Press any key to continue . . .

What is the problem with the above code? Why doesn't it work consistently?

Upvotes: 0

Views: 68

Answers (1)

foxidrive
foxidrive

Reputation: 41234

I altered the indented lines. You need delayed expansion the way it was set up and this way it doesn't require it.

@echo off

set exitval=0

:selectdir
echo Type the name of the folder where the software is installed (or drag and
echo drop the folder here):

set /p sdir=
set sdir=%sdir:"=%

echo.
    if exist "%sdir%\Lib\Plugins\" goto :continue
    set "c="
    set /p "c=The plugins directory does not exist! Try again (y/N)? "
    if /i "%c%" == "y" goto :selectdir
    goto :exitscript

    :continue
    echo do more stuff
    goto :eof


:exitscript
pause
exit /b %exitval%

Upvotes: 1

Related Questions