TurdPile
TurdPile

Reputation: 1036

CMD skips a line in batch file for no apparent reason, so what's the reason?

Simply script, probably a simple question:

set /p customsettings="Some input prompt: "
if /i %customsettings:~0,1% equ Y echo Some output

   ^-This works fine...

set custom=1
if %custom% equ 1 (
    set /p customsettings="Some input prompt: "
    echo Some output
)

   ^-...and this works fine.
So why doesn't this work fine?:

set custom=1
if %custom% equ 1 (
    set /p customsettings="Some input prompt: "
    if /i %customsettings:~0,1% equ Y echo Some output
)

The set /p customsettings line get skipped only when it is pinched between two if-statements.

I'm curious why this happens, and how to fix it.

Note: The problem still persists regardless of EnableDelayedExpansion's setting.

Upvotes: 0

Views: 692

Answers (1)

RGuggisberg
RGuggisberg

Reputation: 4750

You can read a lot about delayed expansion on this site. An entire IF/FOR construct (or multiple lines within parens) are loaded and expanded as 1 line. So you have to consider LOAD-TIME behavior and RUN-TIME behavior. Try this:

@echo off
setlocal enabledelayedexpansion
set custom=1
if %custom% equ 1 (
    set /p customsettings="Some input prompt: "
    if /i "!customsettings:~0,1!"=="Y" echo Some output
)

Upvotes: 2

Related Questions