gabrielnvian
gabrielnvian

Reputation: 73

insert a variable into a variable in batch

I'm trying to insert a variable into a variable, I've tried this, but it didn't work:

set gabriel-ctCODE=fmfg1


set /p user=Username: 
set /p password=Password: 
set /p ctCODE=ctCODE: 



if %password%== %%user%-pass% set /a loginerrorlevel=%loginerrorlevel%+1
pause
if %ctCODE%== "%%user%-ctCODE%" set /a loginerrorlevel=%loginerrorlevel%+1
if not %loginerrorlevel%== 2 goto login.incorrect
goto :aftercommand

I would like to insert the "user" variable into this variable: %%user%-pass%

Could you help me please?

Upvotes: 2

Views: 3335

Answers (1)

Mofi
Mofi

Reputation: 49186

Here is your batch code rewritten for validating entered credential data:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "gabriel-ctCODE=fmfg1"
set "gabriel-pass=xxxyyy"

set "UserName="
set /P "UserName=User name: "
set "Password="
set /P "Password=Password: "
set "ctCODE="
set /P "ctCODE=ctCODE: "

call set "UserPass=%%%UserName%-pass%%"
call set "UserCode=%%%UserName%-ctCODE%%"

set "LoginErrorLevel=0"
if "!Password!" == "!UserPass!" set /A LoginErrorLevel+=1
if "!ctCODE!" == "!UserCode!"   set /A LoginErrorLevel+=1
if not %LoginErrorLevel% == 2 goto LoginIncorrect
echo Entered credential data are valid for login.
goto EndBatch

:LoginIncorrect
echo Enter credential data not valid for login.

:EndBatch
endlocal

On set /P the user of the batch file has the freedom to enter nothing in which case the environment variable keeps its current value or is still not defined if not defined before. The batch code above makes sure that the 3 environment variables are not defined before prompting the user.

The batch file user has also the freedom to enter anything including critical characters for batch file execution like %, > <, ", etc. which is the reason for enclosing the variable=value assignments in double quotes as this results in interpreting the entered characters as literal characters and using delayed expansion on string comparisons.

Read the answers on Why is no string output with 'echo %var%' after using 'set var = text' on command line? and on Password system does not work for more details.

To get the values of the environment variables gabriel-ctCODE and gabriel-pass when the user enters gabriel (in any case) as user name, the command SET must be used with appropriate string and additionally the command CALL to expand the environment variable inside the variable string. Read for example answer on Pass environment variables to subshell CMD for details.

An arithmetic expression is everything after set /A which is interpreted by Windows command line interpreter completely different than usual.

For details on behavior of the commands SETLOCAL and ENDLOCAL read this answer.

For a basic understanding of the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • set /?
  • setlocal /?

Upvotes: 1

Related Questions