Batch file 批处理脚本-如何使用存储了变量名的其他变量获取变量值
我无法访问下面示例中存储的值。我需要访问存储在变量中的值,但变量名存储在不同的变量中。请帮忙 例如:Batch file 批处理脚本-如何使用存储了变量名的其他变量获取变量值,batch-file,Batch File,我无法访问下面示例中存储的值。我需要访问存储在变量中的值,但变量名存储在不同的变量中。请帮忙 例如: setlocal enabledelayedexpansion set a222333password=hellopass for %%i in (%fileserver%\t*) do ( set currentfilename=%%~ni --> file name is "t222333" set currentlogin=!currentfilename:t=a!
setlocal enabledelayedexpansion
set a222333password=hellopass
for %%i in (%fileserver%\t*) do (
set currentfilename=%%~ni --> file name is "t222333"
set currentlogin=!currentfilename:t=a! --> login is "a222333"
set currentpasswd=!!currentlogin!password! --> password should be "hellopass"
echo !currentpasswd! --> this gives me the value "a222333password" instead of "hellopass"
)
不能嵌套延迟扩展,如
set currentpasswd=!!当前登录!密码代码>,因为这首先检测到代码>,合并到一个开口代码>,因此变量展开!当前登录
完成后产生a222333
,然后是文本部分密码
,最后是另一个代码>无法配对,因此被忽略
但是,您可以尝试这样做,因为call
会启动另一个解析阶段:
调用集“currentpasswd=%%!currentlogin!password%%”
或者这是因为
的变量引用在延迟扩展发生之前展开:
对于/F“delims=”(!currentlogin!”)中的%%Z,请设置“currentpasswd=!%%Zpassword!”
或者也是这样,因为参数引用,如正常扩展的变量(%
-expansion),在延迟扩展完成之前进行了扩展:
rem//而不是'set currentpasswd=!!当前登录!密码!`:
调用:子程序currentpasswd“!currentlogin!”
rem//然后,在当前脚本的末尾:
后藤:EOF
:子程序
设置“%~1=!%~2密码!”
后藤:EOF
rem//或者,当您不想将任何参数传递给子程序时:
:子程序
设置“currentpasswd=!%currentlogin%password!”
后藤:EOF
所有这些变体都有两个重要的共同点:
有两个扩展阶段李>
内部变量引用在外部变量引用之前展开李>
虽然主题不同,但在中对这种类型的管理进行了解释。