Problem in batch mode script read user input

i use set /pbelow to read user input this works outside the if block, but inside inside if the block doesn't work. When I run the script a second time, user input in the if block prints the previous user input.

test script:

@echo off
set cond=true
echo %cond%
if %cond%==true (
echo "cond is true"
REM the below input doesn't work
set /p name1="enter your name"
echo name is: %name1%
)

REM it works here
set /p name2="enter your name"
echo name is: %name2%

Thank you

+3
source share
1 answer

Read the extension delay at help set.

(%foo%) , cmd . , . . , , .

,

setlocal enabledelayedexpansion

, , (!foo!), , .

@echo off
setlocal enabledelayedexpansion enableextensions
set cond=true
echo %cond%
if %cond%==true (
echo "cond is true"
REM the below input does work now
set /p name1="enter your name"
echo name is: !name1!
)
+10

Source: https://habr.com/ru/post/1749658/


All Articles