2

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?

4
  • 1
    Your code doesn't work and is indecipherable. Please provide examples. Commented Mar 6, 2017 at 15:49
  • 1
    Why do you have double-% before user? We have no idea what your code is supposed to do (the intent of your code), so we have no idea what proper behavior or "not working" is. Commented Mar 6, 2017 at 15:56
  • Instead %password% == %%user%-pass% you must use %password% == !%user%-pass! with EnableDelayedExpansion. This management is explained at this answer although the topic is different... Commented Mar 6, 2017 at 16:27
  • this code should check if the inserted password (ctCODE) of the choosen user is equal to the saved oneif %ctCODE%== "%%user%-ctCODE%" set /a loginerrorlevel=%loginerrorlevel%+1 This line should have 2 variables in one: %%user%-pass% --> %gabriel-pass% Commented Mar 6, 2017 at 17:58

1 Answer 1

1

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 /?
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.