1

I need help in passing a variable value of a batch file to another batch file.

I am using this statement:

call vartest.bat 

if %username%==NA (
echo First login detected.

set /p usernameIN= Username:
@echo set username=%usernameIN% > vartest.bat
)

The problem is that, the value of "usernameIN" does not pass-on to the external batch file. I tried it using normal text instead of the variable and it works.

Is there any way to make this possible?

Thank you.

Soma Schicksal
  • 11
  • 1
  • 1
  • 7

1 Answers1

0

You need delayed expansion if you want to use a variable, that you changed in the same block (a block is a series of commands within brackets (and ))

setlocal enabledelayedexpansion
call vartest.bat 

if %username%==NA (
  echo First login detected.
  set /p usernameIN= Username:
  @echo set "username=!usernameIN!" > vartest.bat
)

Also you should use set "var=value" to avoid unintended spaces in the variable.

See here for a short demonstration of delayed expansion.

Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91