Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 批处理脚本-如何使用存储了变量名的其他变量获取变量值_Batch File - Fatal编程技术网

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
所有这些变体都有两个重要的共同点:

  • 有两个扩展阶段
  • 内部变量引用在外部变量引用之前展开
    虽然主题不同,但在中对这种类型的管理进行了解释。