0

I am making chat room kind of thing in batch. Unfortunately the file fails to store variables. Here is my code

@echo off
color b
Title Messanger
:top
echo Are you hosting the chat?
set /p host="Yes or no>"
if /i "%host%"=="yes" (
    set /p port="Port>"
    rem This would normally listen for connections
    echo Listening for connections on port %port%
    pause
 )
if /i "%host%"=="no" (
    echo Who is hosting this chat?
    set /p ip="Ip>"
    set /p port="Port>"
    echo ----------------CHAT----------------
    rem This would normally connect to the ip and port
    echo %ip% %port%
 )
echo Error.
pause
goto top

I dont know if you get the same result but only see the message without the variables. OUTPUT:

Are you hosting the chat?
Yes or no>yes
Port>8080
Listening for connections on port
Press any key to continue . . .

After this it does as it should ad echos error and goes to the start of the file

Rob
  • 172
  • 2
  • 3
  • 10
  • Which exact part is not working? I don't have access to a Windows machine but can probably still help if you tell me the desired vs achieved results. Is it the `IF` statement that is failing and not taking the right route, or is it the `%ip%` & `%port%` that are not showing up in the `echo`? – Ruslan Mar 21 '16 at 06:43
  • it is saying that the variables dont have any value when they should take up the value of the input. EDIT: just saw the edit on the comment. the %ip% and %port% are not showing up in the echo – Rob Mar 21 '16 at 06:45
  • So the `IF` statement is not evaluating properly? Or is that working, and it is the `%ip%` and `%port%` that are not showing up in the `echo`? – Ruslan Mar 21 '16 at 06:46
  • Perhaps you can add the output to your question body. – Ruslan Mar 21 '16 at 06:46
  • i can add the output of if i selected no if that helps – Rob Mar 21 '16 at 06:48
  • Thanks! Please see answer. – Ruslan Mar 21 '16 at 06:50

1 Answers1

1

The solution is two-fold:

1) You need to add setlocal enabledelayedexpansion to the beginning of your script. Like this:

@echo off
setlocal enabledelayedexpansion
color b
Title Messanger

(Note line 2)

2) Then, inside the IF blocks, you need to address your variables with ! instead of %, like this:

if /i "%host%"=="yes" (
    set /p port="Port>"
    rem This would normally listen for connections
    echo Listening for connections on port !port!
    pause
 )

(Note line 4)

Explanation

Because %ip% and %port% are inside a clause (i.e. within parentheses), their values get captured before the actual code inside the block executes. Before the interpreter enters the IF block, it captures the value of %port% before doing that (which doesn't exist before it enters the actual block). So, while it looks like your code was running

echo Listening for connections on port %port%

What was actually running is

echo Listening for connections on port 

(Note the empty space. %port% got replaced with the value of %port% before it entered the block within the parentheses - which is an empty value.

The solution for working with variables that get modified and then re-used within one block is to enable delayed expansion and then use the ! symbol instead of % to reference them.

Ruslan
  • 2,691
  • 1
  • 19
  • 29
  • Thanks for the fast response! Will love to see this explanation. Forgot to add that this fixed the issue – Rob Mar 21 '16 at 06:51
  • Great!! Let me know if the explanation makes sense or if you have any other questions. Also, feel free to mark as answer if the solution ended up working correctly. – Ruslan Mar 21 '16 at 06:53
  • 2
    If I got a dollar for every time someone posted a question where the reason the code isn't working is because of delayed Expansion, I'd still be nothing richer because I'm not american and we don't use dollars here. – Dennis van Gils Mar 21 '16 at 13:44