Nested environment variables

后端 未结 2 632
北海茫月
北海茫月 2021-01-23 06:50

I am trying to make a file that asks for your username and password, with a registration. When registering, the passwords are saved in variables.

The problem is i have

2条回答
  •  庸人自扰
    2021-01-23 07:18

    You can use "delayed expansion" to solve the problem of the nested variables. For example, !%user1%pass!: the ! is like % but signals that expansion should be delayed, so %user1% will be expanded first, leaving you with !johnpass! which can be expanded next.

    Here's a complete script:

    @echo off
    setlocal enabledelayedexpansion
    
    set /p "user1=Username: "
    set /p "%user1%pass=Password: "
    
    echo %user1%'s password is !%user1%pass!
    

提交回复
热议问题