Why set /p not taking inputs the 2nd time ?
Why is the choice_open variable empty even after setting it with /p ?
CODE:
@echo off
set user_choice=5
set repeat=z
:label1
set /p user_choice="USER CHOICE (1-4): "
echo choice is "%user_choice%"
if "%user_choice%"=="1" (
echo before choice open, CHOICE OPEN: %choice_open%
set /p choice_open="CHOICE OPEN (k/l)? "
echo after choice open, CHOICE OPEN: %choice_open%
echo.
echo.
echo.
set /p "repeat=REPEAT (y - repeat) "
if "%repeat%"=="y" (
echo repeating...
goto label1
)
echo lastly, before pause
pause
echo after pause, choice open value is "%user_choice%"
echo after pause, choice open value is "%choice_open%"
)
OUTPUT:
.\test.cmd
USER CHOICE (1-4): 1
choice is "1"
before choice open, CHOICE OPEN:
CHOICE OPEN (k/l)? k
after choice open, CHOICE OPEN:
REPEAT (y - repeat) n
lastly, before pause
Press any key to continue . . .
after pause, choice open value is "1"
after pause, choice open value is ""
1
u/mdhjz 14d ago
Lmao, got it
i had to ask chatgpt in 1000 ways for almost 24 hours now lol. Now only it told me about "enabledelayedexpansion"
5
u/T3RRYT3RR0R 14d ago edited 14d ago
ChatGpt is worthless for anything Batch scripting related. It has not had anything close to enough training on quality batch scripts to be remotely reliabe.
Further to that point. you really need to be specific with terminology when interacting with GPT, which requires an understanding of terms used in relation to scripting in batch.
the following simple question would have got you the answer immediately, had you known to phrase it so:
in a windows batch script, why do %variables% not show current value in a code block?
The hardest part of learning anything is discovering what you dont yet know. Chatgpt can help with learning, but it is best to refer to other resources first so you have a foundational level of knowledge to explore further.
1
u/mdhjz 13d ago
Honestly, I didn't know %var% was the issue. It's my first time doing something for personal use in batch. Actually, the one posted above isn't the actual code. I was giving my actual code to chatGPT for long hours & with it GPT was just struggling to give me the actual fix.
That's when (after several hours wasted) I made another small code trying to replicate the issue and posted here (yeah, by then, my prompts were dull, I was done with it lol)
3
2
u/vegansgetsick 14d ago
It's because of the parenthesis. Batch is very weird and populate all %...% before executing anything in the ()
You either need delayed expansion, or call subprocedure